showAnchoredModal function

Future<Object?> showAnchoredModal(
  1. BuildContext context,
  2. Widget content, {
  3. required Rect anchor,
  4. PopoverPlacement placement = api.PopoverPlacement.belowStart,
  5. double gap = 4,
  6. double offset = 0,
  7. double reserve = 0,
  8. bool arrow = false,
  9. bool barrierDismissible = true,
  10. Color? barrierColor,
  11. bool popover = false,
})

Shows content as a popover by anchor (a global rect: the node that opened it, or the pointer as an empty rect) at placement, gap away from it and shifted offset along it. It flips to the other side when it doesn't fit and stays on screen. Below its anchor with a reserve, it doesn't flip: it keeps that much room under its top, moving up when that would pass the screen's bottom margin (a menu that may grow keeps its place; a tall card overlaps its anchor).

Implementation

Future<Object?> showAnchoredModal(
  BuildContext context,
  Widget content, {
  required Rect anchor,
  api.PopoverPlacement placement = api.PopoverPlacement.belowStart,
  double gap = 4,
  double offset = 0,
  double reserve = 0,
  bool arrow = false,
  bool barrierDismissible = true,
  api.Color? barrierColor,
  bool popover = false,
}) {
  final navigator = Navigator.of(context);
  final overlay = navigator.overlay?.context.findRenderObject();
  final local = overlay is RenderBox && overlay.attached
      ? Rect.fromPoints(overlay.globalToLocal(anchor.topLeft), overlay.globalToLocal(anchor.bottomRight))
      : anchor;
  final layout = PopoverLayout(anchor: local, placement: placement, gap: gap, offset: offset,
      reserve: reserve, textDirection: Directionality.of(context));
  final shown = arrow ? _arrowContent(context, content, layout) : content;
  return showShellDialog<Object?>(
    context: context,
    barrierDismissible: barrierDismissible,
    barrierColor: barrierColor == null ? ShellColors.transparent : Color(barrierColor.value),
    useSafeArea: false,
    routeSettings: popover ? const RouteSettings(name: popoverRouteName) : null,
    builder: (_) => CustomSingleChildLayout(
      delegate: layout,
      child: ModalFocus(dismissible: barrierDismissible, popover: true, child: shown),
    ),
  );
}