首页 > 解决方案 > 从 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
      },
  )

如何使弹出菜单在打开时自行重建?

标签: flutterflutter-widgetflutter-state

解决方案


推荐阅读