Flutter Modal Bottom Sheet无法使用AppBar内的弹出菜单按钮



我遇到了一个问题;showModalBottomSheet";不在";onTap";弹出菜单项的功能。单击弹出菜单项时,模态底部工作表不会显示。

这是我在AppBar:的操作参数中的代码

actions: [
PopupMenuButton(
itemBuilder: (BuildContext context) => choices
.map((Choice choice) => PopupMenuItem<Choice>(
child: Row(
children: [
choice.icon,
SizedBox(width: 15),
Text(choice.text),
],
),
value: choice,
onTap: () {
print('Modal Bottom Sheet should open.');
showModalBottomSheet(
context: context,
builder: (context) {
return Container(
color: Colors.transparent,
height: 184,
);
},
);
},
))
.toList())
],

谢谢你的帮助。

如果查看PopupMenuItem文档,您会发现没有onTap方法。相反,您应该使用PopupMenuButtononSelected来检测抽头,如下所示:

actions: [
PopupMenuButton(
onSelected: _onChoiceSelected,
itemBuilder: (BuildContext context) => choices.map((Choice choice) => PopupMenuItem<Choice>(
child: Row(...),
value: choice
)).toList()
)
]
// ...
void _onChoiceSelected(Choice choice) {
showModalBottomSheet<void>(
context: context,
builder: (context) => Container(color: Colors.transparent, height: 184),
);
}

您可以使用WidgetsBinding.instance.addPostFrameCallback打开下一帧中的模态底部表,这里有一个示例

WidgetsBinding.instance.addPostFrameCallback((_) => showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Widget();
},
));

最新更新