Dart 颤振下拉按钮绑定键值数组
如何将如下所示的键值数组绑定到FlatterDropDownButton?我希望键是下拉列表值,值是标签Dart 颤振下拉按钮绑定键值数组,dart,flutter,Dart,Flutter,如何将如下所示的键值数组绑定到FlatterDropDownButton?我希望键是下拉列表值,值是标签 final items = { '1': 'item 1', '2': 'item 2', '3': 'item 3', '4': 'item 4', '5': 'item 5' }; 使用以下命令: DropdownButton<String> button = DropdownB
final items = {
'1': 'item 1',
'2': 'item 2',
'3': 'item 3',
'4': 'item 4',
'5': 'item 5'
};
使用以下命令:
DropdownButton<String> button = DropdownButton(
items: items.entries
.map<DropdownMenuItem<String>>(
(MapEntry<String, String> e) => DropdownMenuItem<String>(
value: e.key,
child: Text(e.value),
))
.toList(),
onChanged: (String newKey) {/* todo handle change */},
);
DropdownButton=DropdownButton(
条目:items.entries
.地图(
(地图条目e)=>下拉菜单项(
值:e.key,
子项:文本(即值),
))
.toList(),
onChanged:(字符串newKey){/*todo handle change*/},
);
很简单,下面的代码片段向您展示了如何
final items = {
'1': 'item 1',
'2': 'item 2',
'3': 'item 3',
'4': 'item 4',
'5': 'item 5'
};
// your list of DropDownMenuItem
List< DropdownMenuItem<String>> menuItems = List();
// loop in the map and getting all the keys
for(String key in items.keys){
menuItems.add(
DropdownMenuItem<String>(
// items[key] this instruction get the value of the respective key
child: Text( items[key] ), // the value as text label
value: key, // the respective key as value
) );
}
//later you will do something like this
DropdownButton<String>(
items: menuItems,
onChanged: (value){
// do your stuffs
},
);
final items={
“1”:“第1项”,
“2”:“第2项”,
“3”:“第3项”,
“4”:“第4项”,
“5”:“第5项”
};
//您的下拉菜单项列表
ListmenuItems=List();
//在地图上循环并获得所有的钥匙
for(项中的字符串键。键){
menuItems.add(
下拉菜单项(
//项[键]此指令获取相应键的值
子项:Text(items[key]),//作为文本标签的值
value:key,//相应的键作为值
) );
}
//以后你会这样做
下拉按钮(
项目:菜单项,
一旦更改:(值){
//做你的事
},
);