关于Flutter:Flutter-选择项目后不会关闭弹出菜单

Flutter - popup menu isn't closed after selecting an item

我正在尝试Flutter,并且PopupMenuButton有问题。在菜单中选择一个项目后,我将显示一个没有菜单的新屏幕,并且在向后导航后,弹出菜单仍处于打开状态-如以下屏幕截图所示:

Popup

1
2
3
4
5
6
7
8
9
10
11
12
13
      actions: <Widget>[
    new PopupMenuButton(
      itemBuilder: (BuildContext context) {
        return <PopupMenuEntry>[
          new AppBarMenuItem("Edit profile", () => Navigator.pushNamed(context, Routes.editProfile)).build(context),
          new AppBarMenuItem("Option 1", () => {}).build(context),
          new AppBarMenuItem("Option 2", () => {}).build(context),
          new AppBarMenuItem("Option 3", () => {}).build(context),
          new AppBarMenuItem("Option 4", () => {}).build(context),
        ];
      },
    ),
  ],

AppBarMenuItem

1
2
3
4
5
new PopupMenuItem(
  child: new InkWell(
    child: new Text(_label),
    onTap: _onTap,
)

如何确保选择一个项目后关闭弹出菜单?好像我只是在PopupMenuButton中使用PopupMenuItem并导航到onSelected功能中的新屏幕一样,菜单正常关闭。但是当我使用InkWellonTap函数时,它不再关闭。


只需在onTap函数Navigator.pop(context,"$popupValue");

中使用pop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
PopupMenuItem<String>(
    value:"Replay Game",
    child: ListTile(
        leading: Icon(Icons.replay,
            color: theme.actionButtonColor),
        title: Text("Replay Game"),
        onTap: () {
            Navigator.pop(context,"Replay Game");
            showDialog(
                context: context,
                builder: (context) {
                    return AlertDialog(
                        content: Text("Clear input and replay game?"),
                        actions: <Widget>[
                            FlatButton(
                                onPressed: () => Navigator.pop(context),
                                child: Text("No"),
                                textColor: theme.alterDialogActionColor,
                            ),
                            FlatButton(
                                onPressed: () {
                                    store.dispatch(ReplayAction(timerBloc, varBloc.fireAnalytics));
                                    Navigator.pop(context);
                                },
                                child: Text("Yes"),
                                textColor: theme.alterDialogActionColor,
                            ),
                        ],
                    );
                });
        },
    ),
)

如文档中所述,当用户从弹出菜单项中选择一个选项时,弹出菜单应自动关闭。
使用InkWell onTap不会自动关闭弹出菜单,而是当从popupMenuList中选择一个项目时直接使用弹出菜单项自动关闭弹出菜单。
确保valuePopupMenuItem的属性不会否则空的onSelectedPopupMenuItem时选择的功能将不会被调用


我遇到了同样的问题,这是我的解决方案。您的PopUpMenuButton()没有利用onSelected属性。 onSelected属性将正确关闭您的PopUpMenuButton。当前不是因为AppBarMenuItemonTap属性接管了该工作。
另外,我创建了一个PopupMenuItem列表,而不是PopUpMenuEntry,不确定是否有区别。但是对于我的每个PopupMenuItem,我还为每个属性分配了value属性,以便onSelected可以与PopUpMenuButton()通信,以窃听哪个项目。

类似这样的内容:

1
2
3
4
5
6
7
8
9
10
11
12
PopupMenuButton(
                onSelected: (selection) {
                  switch (selection) {
                    case 1:
                      ... do stuff...
                      break;
                    case 2:
                      ... break stuff...
                      );
                      break;
                  }
                },

案例1,案例2等指的是我分配给PopUpMenuItem()的value属性。