【发布时间】:2021-01-28 20:02:06
【问题描述】:
我是 Flutter/Dart 的新手,我正试图弄清楚如何在标题中显示某些内容:文本取决于下拉菜单选项的值。我创建了一个带有 DropDownButton 的 ListTile,其中的项目存储为字符串。我想根据 DropDownButton 中选择的字符串显示一个数字。我可以从 setState 中读取值,并根据该字符串输出一个值吗?还是有更好的方法来做到这一点?我已经用谷歌搜索了大约 2 个小时,但似乎无法理解。
class _BarneAnestesiState extends State<BarneAnestesi> {
String Alder;
@override
Widget build(BuildContext context) {
List<Widget> containers = [
Container(
color: Colors.grey,
child: Scaffold(
body: new Column(
children: <Widget> [
new ListTile(
leading: const Icon(Icons.baby_changing_station),
title: new DropdownButton(
hint: Alder == null
? Text('Dropdown')
: Text(
Alder,
style: TextStyle(color: Colors.blue),
),
isExpanded: true,
iconSize: 30.0,
style: TextStyle(color: Colors.blue),
items: ['Nyfødt', '2 mnd.', '1 år', '2 år', '4 år', '7 år'].map(
(val) {
return DropdownMenuItem<String>(
value: val,
child: Text(val),
);
},
).toList(),
onChanged: (val) {
setState(
() {
Alder = val;
},
);
},
)),
new ListTile(
title: new Text(), //I want to display it here. F.eks. 2mnd. should display 3,5mm
),
【问题讨论】: