showAlignedDialog<T> function

Future<T?> showAlignedDialog<T>(
  1. {required BuildContext context,
  2. required WidgetBuilder builder,
  3. bool barrierDismissible = true,
  4. Color? barrierColor = Colors.black54,
  5. String? barrierLabel,
  6. bool useRootNavigator = true,
  7. RouteSettings? routeSettings,
  8. Alignment followerAnchor = Alignment.center,
  9. Alignment targetAnchor = Alignment.center,
  10. Offset offset = Offset.zero,
  11. bool avoidOverflow = false,
  12. bool isGlobal = false,
  13. RouteTransitionsBuilder? transitionsBuilder,
  14. Duration? duration}
)

show a dialog that is aligned to the widget(context) that opens it. The followerAnchor and targetAnchor tells how the dialog and the original widget should be aligned, similar to the CompositedTransformFollower widget.

The offset is for additional fine control over how the dialog is positioned.

avoidOverflow will shift the dialog as possible as it can to avoid painting the dialog outside of the screen, if set to true.

isGlobal, if set to true, will align the dialog relative to the whole screen, rendering the targetAnchor parameter irrelevant.

transitionsBuilder, tells how the dialog shows up and dismisses. The default behavior is a fade transtion, but you can add more animations like sliding easily. duration specifies how long this transtion takes.

Implementation

Future<T?> showAlignedDialog<T>({
  required BuildContext context,
  required WidgetBuilder builder,
  bool barrierDismissible = true,
  Color? barrierColor = Colors.black54,
  String? barrierLabel,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
  Alignment followerAnchor = Alignment.center,
  Alignment targetAnchor = Alignment.center,
  Offset offset = Offset.zero,
  bool avoidOverflow = false,
  bool isGlobal = false,
  RouteTransitionsBuilder? transitionsBuilder,
  Duration? duration,
}) {
  assert(debugCheckHasMaterialLocalizations(context));

  final CapturedThemes themes = InheritedTheme.capture(
    from: context,
    to: Navigator.of(
      context,
      rootNavigator: useRootNavigator,
    ).context,
  );

  final RenderBox targetBox = context.findRenderObject()! as RenderBox;
  final RenderBox overlay =
      Navigator.of(context).overlay!.context.findRenderObject()! as RenderBox;
  Offset position = targetBox
      .localToGlobal(targetAnchor.alongSize(targetBox.size), ancestor: overlay);

  if (isGlobal) {
    position = overlay.localToGlobal(followerAnchor.alongSize(overlay.size));
  }

  return Navigator.of(context, rootNavigator: useRootNavigator)
      .push<T>(AlignedDialogRoute<T>(
    followerAlignment: followerAnchor,
    position: position,
    context: context,
    builder: builder,
    barrierColor: barrierColor,
    barrierDismissible: barrierDismissible,
    barrierLabel: barrierLabel,
    useSafeArea: isGlobal == true,
    settings: routeSettings,
    themes: themes,
    transitionsBuilder: transitionsBuilder,
    duration: duration,
    avoidOverflow: avoidOverflow,
    offset: offset,
  ));
}