showAwaitDialog<T> function Null safety

Future<T?> showAwaitDialog<T>(
  1. BuildContext context,
  2. {Key? key,
  3. required Widget message,
  4. required Future<T> function(
    1. BuildContext context,
    2. dynamic updateMessage(
      1. Widget message
      )
    ),
  5. Color? barrierColor,
  6. bool useSafeArea = true,
  7. bool useRootNavigator = true,
  8. RouteSettings? routeSettings}
)

Displays a dialog with a CircularProgressIndicator while a process is running.

To inform the process that will be executed, use the parameter function, this parameter must receive a function, which can return a value with the result function.

To inform the user what is being executed, you can use the parameter message, it can be updated as necessary during the execution of the function method that is passed as a parameter in the execution of the function.

Below is an example of how to show a dialog and wait for the process to be completed.

final result = await showAwaitDialog<bool>(context,
    message: Text('Creating user'),
    function: (context, updateMessage) async {

    await Future.delayed(Duration(seconds: 2));
    updateMessage(Text('Creating permissions'));
    await Future.delayed(Duration(seconds: 2));

    return true;
});

Implementation

Future<T?> showAwaitDialog<T>(BuildContext context,
    {Key? key,
    required Widget message,
    required Future<T> Function(
            BuildContext context, Function(Widget message) updateMessage)
        function,
    Color? barrierColor,
    bool useSafeArea = true,
    bool useRootNavigator = true,
    RouteSettings? routeSettings}) {
  return showDialog<T>(
      context: context,
      builder: (context) {
        return WillPopScope(
            onWillPop: () => Future.value(false),
            child:
                AwaitDialog<T>(key: key, message: message, function: function));
      },
      barrierDismissible: false,
      barrierColor: barrierColor,
      useSafeArea: useSafeArea,
      useRootNavigator: useRootNavigator,
      routeSettings: routeSettings);
}