Android 如何在运行时在DropDownMenuItems列表中添加新项?
我想在运行时在下拉列表中添加新项Android 如何在运行时在DropDownMenuItems列表中添加新项?,android,flutter,flutter-layout,Android,Flutter,Flutter Layout,我想在运行时在下拉列表中添加新项 Center( child: DropdownButtonHideUnderline( child: DropdownButton( isExpanded: true, value: selectedClass, items: _dropDownMenuItemsClass, onChanged: changedDropDownItem, ), )) var_货币=[‘卢比’、‘美元’、‘英镑’]
Center(
child: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
value: selectedClass,
items: _dropDownMenuItemsClass,
onChanged: changedDropDownItem,
),
))
var_货币=[‘卢比’、‘美元’、‘英镑’]
创建一个要添加字符串的变量。我在这里添加了var\u货币。
希望这对您有用。如果我正确理解了您的问题,请再举一个例子:
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownValue = 'One';
var values = <String>['One', 'Two', 'Free', 'Four'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Row(
children: [
DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: values.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
//Button to add/change value at runtime
RaisedButton(
child: Text('Change'),
onPressed: () {
setState(() {
values = values..add('Value');
});
},
)
],
));
}
}
如果一个新项目在一段时间后会添加值,那么下拉列表是否会更新?如果使用setState,我99%确定应该更新。试试看,让我知道!很高兴听到这个消息!如果您愿意,您可以将我的答案设置为已接受的答案,以便具有相同问题的其他人更好地找到解决方案
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownValue = 'One';
var values = <String>['One', 'Two', 'Free', 'Four'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Title'),
),
body: Row(
children: [
DropdownButtonHideUnderline(
child: DropdownButton<String>(
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: values.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
//Button to add/change value at runtime
RaisedButton(
child: Text('Change'),
onPressed: () {
setState(() {
values = values..add('Value');
});
},
)
],
));
}
}