showDialog<T> function

Future<T?> showDialog<T>({
  1. required BuildContext context,
  2. required RouteWidgetBuilder builder,
  3. bool barrierDismissible = true,
  4. Color? barrierColor,
  5. String? barrierLabel,
  6. bool useSafeArea = true,
  7. Alignment alignment = Alignment.center,
  8. num? width,
  9. num? height,
  10. RouteSettings? routeSettings,
  11. AnimationStyle? animationStyle,
})

Shows a material design dialog.

The dialog route is created and pushed onto the navigator that most closely encloses the given context.

Returns a Future that resolves to the value passed to Navigator.pop when the dialog is dismissed.

final confirmed = await showDialog<bool>(
  context: context,
  builder: (ctx) => DialogConfirm(
    title: 'Delete?',
    message: 'This cannot be undone.',
  ),
);

Implementation

Future<T?> showDialog<T>({
  required BuildContext context,
  required RouteWidgetBuilder builder,
  bool barrierDismissible = true,
  Color? barrierColor,
  String? barrierLabel,
  bool useSafeArea = true,
  Alignment alignment = Alignment.center,
  num? width,
  num? height,
  RouteSettings? routeSettings,
  AnimationStyle? animationStyle,
}) {
  // Capture the caller's theme so the dialog inherits the correct theme
  // even though it renders in the navigator's overlay (which may have a
  // different ThemeScope ancestor).
  final capturedTheme = ThemeScope.maybeOf(context);

  final route = DialogRoute<T>(
    builder: builder,
    barrierDismissible: barrierDismissible,
    barrierColor: barrierColor,
    barrierLabel: barrierLabel,
    useSafeArea: useSafeArea,
    alignment: alignment,
    width: width,
    height: height,
    animationStyle: animationStyle,
    capturedTheme: capturedTheme,
    settings: routeSettings ?? RouteSettings(name: DialogRoute.routeName),
  );

  return Navigator.of(context).push<T>(route);
}