【发布时间】:2020-11-09 11:40:58
【问题描述】:
我创建了一个下拉菜单并用值填充它,但是当我选择一个时,所选值不会改变。我知道这是 SO 上的一个常见问题,但我尝试了很多解决方案,但都没有帮助。这是我的代码:
String selectedOccasion;
@override
Widget build(BuildContext context) {
final collectionProvider = Provider.of<CollectionProvider>(context).allOccasions;
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.deepPurpleAccent,
title: Text('Viewer'),
actions: [
Stack(
children: [
IconButton(
icon: Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Add to collection"),
content: DropdownButton<String>(
hint: Text('Select Occasion'),
value: selectedOccasion,
items: collectionProvider.map((String value) {
return new DropdownMenuItem<String>(
value: value,
child: new Text(value),
);
}).toList(),
onChanged: (String newValue) {
setState(() {
selectedOccasion = newValue;
});
},),
);
});
})
],
)
],
),
body: Container(child: Text('Body')
);
}
有什么建议吗?谢谢
【问题讨论】:
-
尝试在 selectedOccasion 中设置一个默认值(数组值之一)