showPullDownMenu function

Future<void> showPullDownMenu({
  1. required BuildContext context,
  2. required List<PullDownMenuEntry> items,
  3. required RelativeRect position,
  4. required Widget? topWidget,
  5. Size buttonSize = Size.zero,
  6. PullDownMenuPosition menuPosition = PullDownMenuPosition.under,
  7. PullDownMenuItemsOrder itemsOrder = PullDownMenuItemsOrder.downwards,
  8. PullDownMenuCanceled? onCanceled,
  9. PullDownMenuRouteTheme? routeTheme,
})

Displays a pull-down menu with items at position.

items should be not empty for menu to be shown.

If items contains at least one tappable menu item of type PullDownMenuItem.selectable all of PullDownMenuItems should also be of type PullDownMenuItem.selectable.

See https://developer.apple.com/design/human-interface-guidelines/components/menus-and-actions/pull-down-buttons

In order to achieve it all PullDownMenuItems will automatically switch to "selectable" view.

Desired position is used to align top of the menu with top of the position rectangle. buttonSize can be additionally used to let menu know about additional bottom offsets it needs to consider while calculating final menu's position.

menuPosition is used to define whether the popup menu is positioned above, over or under the calculated menu's position. Defaults to PullDownMenuPosition.under.

itemsOrder is used to define how menu will order its items depending on calculated menu's position. Defaults to PullDownMenuItemsOrder.downwards.

onCanceled is called when the user dismisses the pull-down menu.

routeTheme is used to define theme of route used to display pull-down menu launched from this function.

See also:

  • PullDownButton, a default way of displaying a pull-down menu.
  • showMenu, a material design alternative.
  • PullDownMenu, an another alternative way of displaying a pull-down menu.

Implementation

Future<void> showPullDownMenu({
  required BuildContext context,
  required List<PullDownMenuEntry> items,
  required RelativeRect position,
  required Widget? topWidget,
  Size buttonSize = Size.zero,
  PullDownMenuPosition menuPosition = PullDownMenuPosition.under,
  PullDownMenuItemsOrder itemsOrder = PullDownMenuItemsOrder.downwards,
  PullDownMenuCanceled? onCanceled,
  PullDownMenuRouteTheme? routeTheme,
}) async {
  if (items.isEmpty) return;

  final hasLeading = MenuConfig.menuHasLeading(items);

  final action = await _showMenu<VoidCallback>(
    context: context,
    items: items,
    topWidget: topWidget,
    position: position,
    buttonSize: buttonSize,
    menuPosition: menuPosition,
    itemsOrder: itemsOrder,
    routeTheme: routeTheme,
    hasLeading: hasLeading,
  );

  // TODO(notDmDrl): this was not available at Flutter 3.0.0 release,
  // uncomment after min dart version for package is 3.0?
  // if (!context.mounted) return;

  if (action != null) {
    action.call();
  } else {
    onCanceled?.call();
  }
}