我有代码下拉菜单,但我将解析列表项 制作一个下拉菜单模板,所以我只更改列表项部分。
child: DropdownButton<String>(
isExpanded: true,
icon: Icon(Icons.keyboard_arrow_down),
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['Instagram', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
答案 0 :(得分:1)
这是您要做什么吗?
class DropDownTest extends StatefulWidget {
@override
_DropDownTestState createState() => _DropDownTestState();
}
class _DropDownTestState extends State<DropDownTest> {
MenuItem dropdownValue;
@override
Widget build(BuildContext context) {
return DropdownButton<MenuItem>(
isExpanded: true,
icon: Icon(Icons.keyboard_arrow_down),
value: dropdownValue,
onChanged: (MenuItem newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: items.map<DropdownMenuItem<MenuItem>>((MenuItem value) {
return DropdownMenuItem<MenuItem>(
value: value,
child: Text(value.name),
);
}).toList());
}
}
class MenuItem {
final int id;
final String name;
const MenuItem(this.id, this.name);
}
const List<MenuItem> items = [
MenuItem(1, 'ONE'),
MenuItem(2, 'TWO'),
MenuItem(3, 'THREE'),
MenuItem(4, 'FOUR'),
];