Flutter - popup menu isn't closed after selecting an item
我正在尝试Flutter,并且

从编辑个人资料屏幕(第二个屏幕截图)导航回到帐户屏幕(第一个屏幕截图)后,弹出菜单仍然打开。我希望将其关闭。我在应用栏内创建弹出菜单的相关代码:
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), ]; }, ), ], |
和
1 2 3 4 5 | new PopupMenuItem( child: new InkWell( child: new Text(_label), onTap: _onTap, ) |
如何确保选择一个项目后关闭弹出菜单?好像我只是在
只需在
中使用
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, ), ], ); }); }, ), ) |
如文档中所述,当用户从弹出菜单项中选择一个选项时,弹出菜单应自动关闭。
使用
确保
我遇到了同样的问题,这是我的解决方案。您的
另外,我创建了一个
类似这样的内容:
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等指的是我分配给