【发布时间】:2021-05-25 08:53:26
【问题描述】:
【问题讨论】:
标签: flutter dart dropdown textformfield
【问题讨论】:
标签: flutter dart dropdown textformfield
可能使用 DropdownButton 类,https://api.flutter.dev/flutter/material/DropdownButton-class.html
在文档页面上,您可以看到不包含图标的示例代码。
您可以通过将 items 参数更改为让 Row 包装图标和文本小部件来向下拉项目添加图标。
见:https://dartpad.dev/b742bde3465a616f8787c434415c9e3e?null_safety=true
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Row(
children: [
Icon(Icons.close),
Text(value),
],
),
);
}).toList(),
【讨论】:
...
//custom object
class Item{
String name; //set the item name
Icon icon; //set corresponding icon
}
Item selectedItem;
List<Item> itemList = []; //create a list of the items, you need to add items into it first
DropdownButton<Item>(
isExpanded: true,
hint: new Text("Select Item"),
value: selectedItem ,
icon: Icon(Icons.arrow_drop_down_circle),
iconSize: 24,
elevation: 16,
style: TextStyle(color: Colors.blueAccent),
underline: Container(
height: 2,
color: Colors.blue,
),
onChanged: (Item newValue) {
setState(() {
selectedItem = newValue;
});
},
items: itemList.map<DropdownMenuItem<Item>>((Item value) {
return DropdownMenuItem<Item>(
value: value,
child: Row(
//set alignment here
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Icon(value.icon), //in here you can display the icon
Text(value.name), //name of the item
],
);
}).toList(),
)
【讨论】: