showShadDialog<T> function

Future<T?> showShadDialog<T>({
  1. required BuildContext context,
  2. required WidgetBuilder builder,
  3. bool barrierDismissible = true,
  4. Color barrierColor = const Color(0xcc000000),
  5. String barrierLabel = '',
  6. bool useRootNavigator = true,
  7. RouteSettings? routeSettings,
  8. Offset? anchorPoint,
  9. List<Effect>? animateIn,
  10. List<Effect>? animateOut,
  11. ShadDialogVariant variant = ShadDialogVariant.primary,
  12. bool opaque = false,
})

Displays a ShadDialog as a modal dialog with animation.

Shows a dialog with customizable barrier and animation properties, returning a Future with the result when the dialog is dismissed.

Implementation

Future<T?> showShadDialog<T>({
  /// The build context in which to show the dialog.
  required BuildContext context,

  /// The builder function to create the dialog content.
  required WidgetBuilder builder,

  /// Whether the dialog can be dismissed by tapping outside the barrier.
  /// Defaults to true.
  bool barrierDismissible = true,

  /// The color of the barrier behind the dialog.
  /// Defaults to a semi-transparent black (0xcc000000).
  Color barrierColor = const Color(0xcc000000),

  /// The accessibility label for the barrier.
  /// Defaults to an empty string.
  String barrierLabel = '',

  /// Whether to use the root navigator for routing.
  /// Defaults to true.
  bool useRootNavigator = true,

  /// Optional route settings for navigation.
  RouteSettings? routeSettings,

  /// The anchor point for positioning the dialog.
  Offset? anchorPoint,

  /// The animation effects when the dialog appears.
  /// Defaults to fade and scale-in if not specified.
  List<Effect<dynamic>>? animateIn,

  /// The animation effects when the dialog disappears.
  /// Defaults to fade and scale-out if not specified.
  List<Effect<dynamic>>? animateOut,

  /// The variant of the dialog to display.
  /// Defaults to [ShadDialogVariant.primary].
  ShadDialogVariant variant = ShadDialogVariant.primary,

  /// Whether the route occludes the routes behind it.
  /// When false, [MediaQuery.viewInsetsOf] from the host scaffold will be
  /// available in the dialog context, allowing keyboard-aware content.
  /// Defaults to true (standard dialog behavior).
  bool opaque = false,
}) {
  final theme = ShadTheme.of(context);
  final effectiveDialogTheme = switch (variant) {
    ShadDialogVariant.primary => theme.primaryDialogTheme,
    ShadDialogVariant.alert => theme.alertDialogTheme,
  };

  final effectiveAnimateIn =
      animateIn ??
      effectiveDialogTheme.animateIn ??
      const [
        FadeEffect(),
        ScaleEffect(begin: Offset(.95, .95), end: Offset(1, 1)),
      ];

  final effectiveAnimateOut =
      animateOut ??
      effectiveDialogTheme.animateOut ??
      const [
        FadeEffect(begin: 1, end: 0),
        ScaleEffect(begin: Offset(1, 1), end: Offset(.95, .95)),
      ];

  /// Returns the maximum duration(including delay) from
  /// the list of [Effect]s.
  Duration maxCompletionDuration(List<Effect<dynamic>> effects) {
    if (effects.isEmpty) return Duration.zero;

    return effects.fold<Duration>(
      Duration.zero, // start with zero
      (max, effect) {
        final effectTotal =
            (effect.delay ?? Duration.zero) +
            (effect.duration ?? Animate.defaultDuration);
        return effectTotal > max ? effectTotal : max;
      },
    );
  }

  return Navigator.of(context, rootNavigator: useRootNavigator).push(
    ShadDialogRoute(
      pageBuilder: builder,
      barrierColor: barrierColor,
      barrierDismissible: barrierDismissible,
      barrierLabel: barrierLabel,
      anchorPoint: anchorPoint,
      settings: routeSettings,
      opaque: opaque,
      transitionDuration: maxCompletionDuration(effectiveAnimateIn),
      reverseTransitionDuration: maxCompletionDuration(effectiveAnimateOut),
      transitionBuilder: (context, animation, secondaryAnimation, child) {
        if (animation.status == AnimationStatus.completed) {
          return child;
        }
        final animateIn = animation.status == AnimationStatus.forward;
        return ShadAnimate(
          effects: animateIn ? effectiveAnimateIn : effectiveAnimateOut,
          child: child,
        );
      },
    ),
  );
}