showButtonMenu method Null safety

void showButtonMenu()

A method to show a popup menu with the items supplied to PopupMenuButton.itemBuilder at the position of your PopupMenuButton.

By default, it is called when the user taps the button and PopupMenuButton.enabled is set to true. Moreover, you can open the button by calling the method manually.

You would access your PopupMenuButtonState using a GlobalKey and show the menu of the button with globalKey.currentState.showButtonMenu.


void showButtonMenu() {
  final PopupMenuThemeData popupMenuTheme = PopupMenuTheme.of(context);
  final RenderBox button = context.findRenderObject()! as RenderBox;
  final RenderBox overlay = Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;
  final Offset offset;
  switch (widget.position) {
    case PopupMenuPosition.over:
      offset = widget.offset;
    case PopupMenuPosition.under:
      offset = Offset(0.0, button.size.height - (widget.padding.vertical / 2)) + widget.offset;
  final RelativeRect position = RelativeRect.fromRect(
      button.localToGlobal(offset, ancestor: overlay),
      button.localToGlobal(button.size.bottomRight( + offset, ancestor: overlay),
    ), & overlay.size,
  final List<PopupMenuEntry<T>> items = widget.itemBuilder(context);
  // Only show the menu if there is something to show
  if (items.isNotEmpty) {
      context: context,
      elevation: widget.elevation ?? popupMenuTheme.elevation,
      items: items,
      initialValue: widget.initialValue,
      position: position,
      shape: widget.shape ?? popupMenuTheme.shape,
      color: widget.color ?? popupMenuTheme.color,
      constraints: widget.constraints,
    .then<void>((T? newValue) {
      if (!mounted)
        return null;
      if (newValue == null) {
        return null;