showPopupDialog<T> function

Future<T?> showPopupDialog<T>(
  1. BuildContext context,
  2. WidgetBuilder builder, {
  3. bool useRootNavigator = false,
  4. bool barrierDismissible = true,
  5. Color? barrierColor,
  6. String? barrierLabel,
  7. bool asDropDown = false,
  8. bool useTargetWidth = false,
  9. double? dialogWidth,
})

Show a popup dialog

The context is the target context. This argument is required

The builder will be invoked to build the dialog. This argument is required

The useRootNavigator argument is used to determine whether to push the dialog to the Navigator furthest from or nearest to the given context. By default, useRootNavigator is true and the dialog route created by this method is pushed to the root navigator

The barrierDismissible argument is used to indicate whether tapping on the barrier will dismiss the dialog. Default is true

The barrierColor argument is used to specify the color of the modal barrier that darkens everything below the dialog

The asDropDown argument will show the dialog as dropdown.

The useTargetWidth will show the dialog as the same width as the target

The dialogWidth argument define the width of dialog

Implementation

Future<T?> showPopupDialog<T>(
  BuildContext context,
  WidgetBuilder builder, {
  bool useRootNavigator = false,
  bool barrierDismissible = true,
  Color? barrierColor,
  String? barrierLabel,
  bool asDropDown = false,
  bool useTargetWidth = false,
  double? dialogWidth,
}) {
  final navigator = Navigator.of(context, rootNavigator: useRootNavigator);
  final renderBox = context.findRenderObject()! as RenderBox;
  final overlayRenderBox = navigator.overlay!.context.findRenderObject()! as RenderBox;

  final position = RelativeRect.fromRect(
    Rect.fromPoints(
      renderBox.localToGlobal(renderBox.size.topLeft(Offset.zero), ancestor: overlayRenderBox),
      renderBox.localToGlobal(renderBox.size.bottomRight(Offset.zero), ancestor: overlayRenderBox),
    ),
    Offset.zero & overlayRenderBox.size,
  );

  return navigator.push(_PopupDialogRoute<T>(
    asDropDown: asDropDown,
    position: position,
    capturedThemes: InheritedTheme.capture(from: context, to: navigator.context),
    dialogWidth: useTargetWidth ? renderBox.size.width : dialogWidth,
    barrierDismissible: barrierDismissible,
    barrierColor: barrierColor,
    barrierLabel: barrierLabel,
    child: builder(context),
  ));
}