首页 > 解决方案 > 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),
          ),
        ),
      ],
    );
  }
}

标签: iosflutterdart

解决方案


推荐阅读