ios - Flutter PopupMenuButton onSelected 未触发
问题描述
PopupMenuButton onSelected 在 iPhone 11 上没有被调用,即使我添加了断点,它也不起作用。当我单击 PopupMenuItem 时,PopupMenuButton 就消失了。
enum FeedMoreAction { Report, Block }
class UserInfoWidget extends StatefulWidget {
@override
_UserInfoWidgetState createState() => _UserInfoWidgetState();
}
class _UserInfoWidgetState extends State<UserInfoWidget> {
final GlobalKey _popupButtonKey = GlobalKey<State>();
final List<PopupMenuEntry> _menuEntries = [
PopupMenuItem<FeedMoreAction>(
value: FeedMoreAction.Report,
child: Text('Report'),
),
PopupMenuItem(
value: FeedMoreAction.Block,
child: Text('Block'),
),
];
@override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
PopupMenuButton<FeedMoreAction>(
onSelected: (FeedMoreAction action) async {
switch (action) {
case FeedMoreAction.Report:
showDialog(
context: context,
barrierDismissible: false,
child: AlertDialog(
title: Text('Are your sure?'),
content: Container(
height: 50,
child: Text('Are you sure you want to report this?')),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('Yes')),
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('No')),
],
));
break;
case FeedMoreAction.Block:
break;
default:
}
},
itemBuilder: (_) => _menuEntries,
key: _popupButtonKey,
child: GestureDetector(
onTap: () async {
// Here we get the render object of our physical button, later to get its size & position
final RenderBox popupButtonObject =
_popupButtonKey.currentContext.findRenderObject();
// Get the render object of the overlay used in `Navigator` / `MaterialApp`, i.e. screen size reference
final RenderBox overlay =
Overlay.of(context).context.findRenderObject();
// Calculate the show-up area for the dropdown using button's size & position based on the `overlay` used as the coordinate space.
final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
popupButtonObject.localToGlobal(Offset.zero,
ancestor: overlay),
popupButtonObject.localToGlobal(
popupButtonObject.size.bottomRight(Offset.zero),
ancestor: overlay),
),
Offset.zero &
overlay
.size, // same as: new Rect.fromLTWH(0.0, 0.0, overlay.size.width, overlay.size.height)
);
await showMenu(
context: context, position: position, items: _menuEntries);
},
child: Icon(Icons.more_horiz_outlined),
),
),
],
);
}
}
解决方案
推荐阅读
- c++ - 如何使用文件句柄写入特定偏移量
- android - 如何清除Android剪贴板?
- youtube-data-api - youtube api 显示错误,如计数
- wso2 - 所需的捆绑清单标头不存在 wso2
- python - 在 DB 扫描算法中显示名称而不是坐标
- python - Pandas DF:如何用分隔符(;CSV 格式)将 '-' 替换为 '_'
- .net - clr profiler - 添加没有 GAC 问题的辅助 dll
- reactjs - ReactJS 返回保存的响应并传递给不同的组件
- python - 403 - 在 Windows 上使用 mod_wsgi 禁止 Apache 和 Django
- python-3.x - Python:多行参数