showExpressiveMenu function

Future<void> showExpressiveMenu(
  1. BuildContext context, {
  2. Rect? anchor,
  3. List<ExpressiveMenuGroup>? groups,
  4. List<ExpressiveMenuEntry>? entries,
  5. bool vibrant = false,
  6. ExpressiveComponentThemeData? style,
  7. ExpressiveMenuPosition position = ExpressiveMenuPosition.below,
  8. String? label,
  9. bool matchAnchorWidth = false,
  10. double? maxHeight,
})

Opens a menu of groups (or one group of entries) against anchor, a rectangle in global coordinates, or against context's own box (Material's showMenu, Compose's DropdownMenu); resolves when it closes. Closed with nothing chosen (Escape, a tap outside, Back), it gives the focus back to what opened it under context, even a button the pointer pressed, so Enter opens it again.

showExpressiveMenu(context, entries: [ExpressiveMenuItem('Rename', onPressed: rename)])

Implementation

Future<void> showExpressiveMenu(
  BuildContext context, {
  Rect? anchor,
  List<ExpressiveMenuGroup>? groups,
  List<ExpressiveMenuEntry>? entries,
  bool vibrant = false,
  ExpressiveComponentThemeData? style,
  ExpressiveMenuPosition position = ExpressiveMenuPosition.below,
  String? label,
  bool matchAnchorWidth = false,
  double? maxHeight,
}) async {
  assert((groups == null) != (entries == null));
  if (vibrant) {
    assert(
      ExpressiveHeroMoment.debugCheck(context, 'ExpressiveMenu (vibrant)'),
    );
  }
  final resolved =
      groups ?? <ExpressiveMenuGroup>[ExpressiveMenuGroup(entries!)];
  final opener = _openerUnder(context);
  final chosen = await _showMenuRoute(
    context,
    anchor: anchor,
    position: position,
    label: label,
    matchAnchorWidth: matchAnchorWidth,
    maxHeight: maxHeight,
    listKeys: true,
    focusItem: _openKeyHeld(),
    content: (context) => ExpressiveMenuSurface._popup(
      groups: resolved,
      vibrant: vibrant,
      style: style,
    ),
  );
  if (chosen == true || opener == null || !context.mounted) return;
  if (!(ModalRoute.isCurrentOf(context) ?? true)) return;
  if (opener.context?.mounted == true && opener.canRequestFocus) {
    opener.requestFocus();
  }
}