[英]Flutter DropDownButton won't display selected value even when I update it
[英]Flutter: Dropdownbutton does not update to display selected choice
我正在尝试使用下拉按钮让用户 select 从选项列表中,但是在做出选择后,下拉按钮仍然显示提示。 我认为 setState 的某些内容没有更新下拉按钮。
value: skillChoice,
items: listDrop,
hint: Text("Choose Skill"),
onChanged: (value) {
setState(() {
skillChoice = value;
});
},
),
以下是代码中前面声明的变量:
List<DropdownMenuItem<int>> listDrop = [];
int skillChoice = null;
谁能告诉我为什么不更新?
我认为设置 SkillChoice null 最初会禁用下拉菜单。
如果您展示了如何实现 DropDownButton 的完整代码片段,那就更好了。 但这是我如何实现我的:
// This is the initial value that will be selected on the DropDownMenu by default
String choice = '1';
// This is the List of String (or whatever data type you want) that will make up the
Drop Down Menu Item options. NOTE: That the String value of 'choice' ('1') is also present in the List of choices ('1')
List<String> choices = [
'1',
'2',
'3',
'4',
'5',
];
DropdownButton<String>(
value: choice,
icon: Icon(Icons.add),
onChanged: (String newValue) {
setState(() => choices = newValue);
},
items: choices.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value,
)
);
}).toList(),
),
现在应该为您正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.