showIndicator method

FutureOr<T?> showIndicator(
  1. BuildContext context, {
  2. Color? barrierColor = Colors.black54,
  3. Widget? indicator,
})

Indicators are displayed for Future and FutureOr.

Blocks user operation until Future is finished. Darkens the screen and displays an indicator in the center.

It is possible to change the color of the entire screen by specifying barrierColor.

The indicator can be changed by specifying indicator.

By default, CircularProgressIndicator is used.

FutureFutureOrに対してインジケーターを表示します。

Futureが終了するまで、ユーザーの操作をブロック。画面を暗くして中央にインジケーターを表示します。

barrierColorを指定して画面全体の色を変更することが可能です。

indicatorを指定するとインジケーターを変更することが可能です。

デフォルトだとCircularProgressIndicatorが利用されます。

Future.delayed(const Duration(seconds: 1)).showIndicator(context);

Implementation

FutureOr<T?> showIndicator(
  BuildContext context, {
  Color? barrierColor = Colors.black54,
  Widget? indicator,
}) async {
  final futureOr = this;
  DialogRoute<T>? route;
  Completer<void>? completer;
  if (futureOr is Future<T>) {
    completer = Completer<void>();
    var navigator = Navigator.of(context, rootNavigator: true);
    final rootContext = navigator.context;
    futureOr.whenComplete(
      () async {
        completer?.complete();
        completer = null;
        if (route != null) {
          navigator.removeRoute(route!);
          route = null;
        }
      },
    );
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (completer == null) {
        return;
      }
      try {
        final themes = InheritedTheme.capture(
          from: context,
          to: rootContext,
        );
        route = DialogRoute<T>(
          context: rootContext,
          builder: (_) {
            return PopScope(
              canPop: false,
              child: indicator ??
                  Center(
                    child: CircularProgressIndicator(
                      backgroundColor: Colors.white.withOpacity(
                        0.5,
                      ),
                    ),
                  ),
            );
          },
          barrierColor: barrierColor,
          barrierDismissible: false,
          useSafeArea: true,
          themes: themes,
          traversalEdgeBehavior: TraversalEdgeBehavior.closedLoop,
        );
        if (route != null) {
          navigator.push<T>(route!);
        }
      } catch (e) {
        completer?.completeError(e);
        completer = null;
      } finally {
        completer?.complete();
        completer = null;
      }
    });
    await completer?.future;
    final value = await this;
    return value;
  } else {
    return await this;
  }
}