flutter - Flutter - 选择项目后弹出菜单未关闭
问题描述
我正在尝试使用 Flutter 并遇到PopupMenuButton
. 在菜单中选择一个项目后,我会显示一个没有菜单的新屏幕,并且在我导航回来后,弹出菜单仍处于打开状态 - 如以下屏幕截图所示:
从编辑配置文件屏幕(第二个屏幕截图)导航回帐户屏幕(第一个屏幕截图)后,弹出菜单仍然打开。我希望它被关闭。我在应用栏中创建弹出菜单的相关代码:
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
:
new PopupMenuItem(
child: new InkWell(
child: new Text(_label),
onTap: _onTap,
)
如何确保选择项目后弹出菜单关闭?看起来如果我只是PopupMenuItem
在我的菜单中使用PopupMenuButton
并导航到onSelected
功能中的新屏幕,菜单就会正确关闭。但是当我使用它的onTap
功能时,InkWell
它不再关闭。
解决方案
只需pop
在你的onTap
函数中使用Navigator.pop(context, "$popupValue");
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,
),
],
);
});
},
),
)