flutter - 如何为我的下拉菜单添加自定义选项?
问题描述
所以现在我有一个从 api 获取的下拉列表。这就是我所做的
Future<String> getSWData() async {
var res = await http.get(
Uri.encodeFull(Configuration.url + "api/getCategories"),
headers: {"Accept": "application/json"});
var resBody = json.decode(res.body);
setState(() {
data = resBody;
});
print(data);
return "Sucess";
}
那么这就是我在下拉列表中使用它的方式
DropdownButtonHideUnderline(
child: new DropdownButton(
hint: new Text("Choose Category"),
items: data.map((item) {
return new DropdownMenuItem(
child: new Text(item['categoryName']),
value: item['categoryId'].toString(),
);
}).toList(),
onChanged: (newVal) {
setState(() {
mySelection = newVal;
});
},
value: _mySelection, ),),
一切正常,但现在我想添加All Category
到我的下拉列表中,我该如何实现?提前致谢
解决方案
您的 DropdownButton 项目是一个列表,因此您可以使用 ..add 添加到该列表
DropdownButtonHideUnderline(
child: new DropdownButton(
hint: new Text("Choose Category"),
items: data.map((item) {
return new DropdownMenuItem(
child: new Text(item['categoryName']),
value: item['categoryId'].toString(),
);
}).toList()
..add(DropdownMenuItem(child: Text("All Category"), value: 'allcategory')),
onChanged: (newVal) {
setState(() {
mySelection = newVal;
});
},
value: _mySelection, ),),
推荐阅读
- hlsl - 为什么衰减光的强度会出现不平滑的降低?
- c++ - 我可以使用普通的冒泡排序来按字母顺序排列列表吗?
- ruby-on-rails - 无需储蓄卡即可进行条带支付
- batch-file - 获取具有特定扩展名的所有文件
- reactjs - 创建反应本机应用程序时出错
- python - 如何使用 Python 从 CSV 创建嵌套字典
- java - simpMessagingTemplate convertAndSendToUser 大量等待线程阻塞其他功能
- javascript - 无法在我的蛇游戏上动态显示分数
- python - 如何在 sympy 中获得数字输出
- sql-server - 按日期过滤记录