showButtonMenu method

void showButtonMenu()

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

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

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

Implementation

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 RelativeRect position = RelativeRect.fromRect(
    Rect.fromPoints(
      button.localToGlobal(widget.offset, ancestor: overlay),
      button.localToGlobal(
          button.size.bottomRight(Offset.zero) + widget.offset,
          ancestor: overlay),
    ),
    Offset.zero & overlay.size,
  );
  final List<MongolPopupMenuEntry<T>> items = widget.itemBuilder(context);
  // Only show the menu if there is something to show
  if (items.isNotEmpty) {
    widget.onOpened?.call();
    showMongolMenu<T?>(
      context: context,
      elevation: widget.elevation ?? popupMenuTheme.elevation,
      shadowColor: widget.shadowColor ?? popupMenuTheme.shadowColor,
      surfaceTintColor:
          widget.surfaceTintColor ?? popupMenuTheme.surfaceTintColor,
      items: items,
      initialValue: widget.initialValue,
      position: position,
      shape: widget.shape ?? popupMenuTheme.shape,
      color: widget.color ?? popupMenuTheme.color,
      constraints: widget.constraints,
      clipBehavior: widget.clipBehavior,
      useRootNavigator: widget.useRootNavigator,
      popUpAnimationStyle: widget.popUpAnimationStyle,
    ).then<void>((T? newValue) {
      if (!mounted) return null;
      if (newValue == null) {
        widget.onCanceled?.call();
        return null;
      }
      widget.onSelected?.call(newValue);
    });
  }
}