showProgressDialog<T> function

Future<ProgressDialogResult<T>> showProgressDialog<T>({
  1. required BuildContext context,
  2. required Task<T> future,
  3. WidgetBuilder? builder,
  4. bool useRootNavigator = true,
  5. Offset? anchorPoint,
  6. String? barrierLabel,
  7. bool? requestFocus,
  8. TraversalEdgeBehavior? traversalEdgeBehavior,
  9. Color? barrierColor,
  10. bool useSafeArea = true,
  11. bool fullscreenDialog = false,
  12. AnimationStyle? animationStyle,
})

Shows a progress dialog while executing a Future task.

This function displays a modal progress dialog that remains visible until the provided future completes. The dialog can be customized using the builder parameter.

Parameters:

  • context - The build context used to show the dialog
  • future - The async task to execute while showing the progress dialog
  • builder - Optional custom widget builder for the progress dialog UI
  • useRootNavigator - Whether to show dialog above all screens, defaults to true
  • anchorPoint - Optional anchor point for the dialog position
  • barrierLabel - Semantic label used for the modal barrier
  • requestFocus - Whether the dialog should request focus when opened
  • traversalEdgeBehavior - Determines dialog edge behavior when using keyboard traversal
  • barrierColor - Color of the modal barrier, defaults to black54
  • useSafeArea - Whether to respect system UI safe areas, defaults to true
  • fullscreenDialog - Whether this dialog is a fullscreen dialog
  • animationStyle - Style of the dialog animation

Returns a ProgressDialogResult containing either the successful result value or error details if the future fails.

Example:

final result = await showProgressDialog(
  context: context,
  future: () => myAsyncTask(),
);

switch (result) {
  case Success(value: final value):
    print('Task completed successfully: $value');
    break;
  case Failure(error: final error):
    print('Task failed: $error');
    break;
}

Implementation

Future<ProgressDialogResult<T>> showProgressDialog<T>({
  required m.BuildContext context,
  required Task<T> future,
  m.WidgetBuilder? builder,
  bool useRootNavigator = true,
  m.Offset? anchorPoint,
  String? barrierLabel,
  bool? requestFocus,
  m.TraversalEdgeBehavior? traversalEdgeBehavior,
  m.Color? barrierColor,
  bool useSafeArea = true,
  bool fullscreenDialog = false,
  m.AnimationStyle? animationStyle,
}) async {
  final themes = m.InheritedTheme.capture(
    from: context,
    to: m.Navigator.of(context, rootNavigator: useRootNavigator).context,
  );

  late final m.Route<ProgressDialogResult<T>> route;

  route = m.DialogRoute<ProgressDialogResult<T>>(
    context: context,
    builder: (context) => ExactlyOnce(
      callback: () => _callback(context, future, route),
      child: builder?.call(context) ?? const ProgressBarDialog(),
    ),
    barrierColor: barrierColor ??
        m.DialogTheme.of(context).barrierColor ??
        m.Theme.of(context).dialogTheme.barrierColor ??
        m.Colors.black54,
    barrierDismissible: false,
    barrierLabel: barrierLabel,
    useSafeArea: useSafeArea,
    themes: themes,
    anchorPoint: anchorPoint,
    traversalEdgeBehavior:
        traversalEdgeBehavior ?? m.TraversalEdgeBehavior.closedLoop,
    requestFocus: requestFocus,
    animationStyle: animationStyle,
    fullscreenDialog: fullscreenDialog,
  );

  final result = await m.Navigator.of(context, rootNavigator: useRootNavigator)
      .push<ProgressDialogResult<T>>(route);

  return result!;
}