flutteroverridingflutter-layoutdart-pubpopupmenubutton

How not to dismiss a PopUpMenuButton after selecting an item?


I am using flutter PopUpMenuButton. All i want is when i select any item on the menu, the popup should not be dismissed, rather let me select multiple values from the popup.The documentation says that you can override the handleTap property, but it is unclear for me how to do that? This is documented

 ///The [handleTap] method can be overridden to adjust exactly what happens when
/// the item is tapped. By default, it uses [Navigator.pop] to return the
/// [PopupMenuItem.value] from the menu route.

    void handleTap() {
    Navigator.pop<T>(context, widget.value);
  }

Solution

  • Create a custom class, say PopupItem, which extends PopupMenuItem and override PopupMenuItemState.handleTap method.

    class PopupItem extends PopupMenuItem {
      const PopupItem({
        required Widget child,
        Key? key,
      }) : super(key: key, child: child);
    
      @override
      _PopupItemState createState() => _PopupItemState();
    }
    
    class _PopupItemState extends PopupMenuItemState {
      @override
      void handleTap() {}
    }
    

    You can now use it like this:

    PopupMenuButton(
      itemBuilder: (_) {
        return [
          PopupItem(child: ...),
        ];
      },
    )