flutter - 从 PopupMenuButton 打开时删除项目
问题描述
我有一个PopupMenuButton
显示一些PopupMenuItem<String>
从List<String>
. 每个项目都有一个删除按钮,用于String
从列表中删除。问题是弹出菜单在删除项目后不会重建,直到它关闭并再次打开。
似乎无论我做什么,即使使用 aGlobalKey
和调用key.currentState.setState()
,它都不会导致弹出菜单被重建,直到它关闭并再次打开。
GlobalKey _favoritesKey = new GlobalKey();
PopupMenuButton<String>(
key: _favoritesKey,
icon: Icon(Icons.bookmark_border),
itemBuilder: (context){
List<PopupMenuItem<String>> result = [];
model.favorites.forEach((x){
result.add(PopupMenuItem<String>(value: x, child: Row(
children: [
IconButton(icon: Icon(Icons.delete_outline), onPressed: (){
model.removeFavorite(x);
_favoritesKey.currentState?.setState((){});
setState(() {});
}),
Text(x)
]
)));
});
return result;
},
onSelected: (x){
// Do something with the selected value
},
)
如何使弹出菜单在打开时自行重建?
解决方案
推荐阅读
- json - 如何使用序列化程序(Symfony 4)创建正确格式的 json 字符串?
- angular - 错误处理程序和 RxJS 6.2.2
- c# - 时间:2019-01-01 标签:c#automapper deep nested objects to flat object list
- mongodb - 从 mongo 集合中检索对象中的数据
- c# - 如何在 C# 中将消息格式从 MQHRF2 更改为字符串格式
- sql-server - 如何使用 sql server 在 sp 中显示带撇号的名称
- haskell - 在 Haskell 中命名没有重复名称的树(带子)结构的最佳方法
- javascript - 使用 Google Maps API,如何在当前缩放级别上用所有可见标记填充 html 表格
- mysql - MySql Server 触发异常处理
- c++ - 更喜欢 std::swap 到容器的 swap()?