show<T> static method

Future<T?> show<T>({
  1. required BuildContext context,
  2. required WidgetBuilder builder,
  3. String tag = _safeDialogDefaultTag,
  4. bool barrierDismissible = true,
  5. Color? barrierColor = Colors.black54,
  6. String? barrierLabel,
  7. bool useSafeArea = true,
  8. bool useRootNavigator = true,
  9. RouteSettings? routeSettings,
})

展示 Dialog tag : 用于标记 Dialog 类型

Implementation

static Future<T?> show<T>({
  required BuildContext context,
  required WidgetBuilder builder,
  String tag = _safeDialogDefaultTag,
  bool barrierDismissible = true,
  Color? barrierColor = Colors.black54,
  String? barrierLabel,
  bool useSafeArea = true,
  bool useRootNavigator = true,
  RouteSettings? routeSettings,
}) {
  assert(debugCheckHasMaterialLocalizations(context));
  final CapturedThemes themes = InheritedTheme.capture(
    from: context,
    to: Navigator.of(
      context,
      rootNavigator: useRootNavigator,
    ).context,
  );

  _SafeDialogRoute<T> safeDialogRoute = _SafeDialogRoute<T>(
    context: context,
    builder: builder,
    barrierColor: barrierColor,
    barrierDismissible: barrierDismissible,
    barrierLabel: barrierLabel,
    useSafeArea: useSafeArea,
    settings: routeSettings,
    themes: themes,
  );

  // Notice:
  // 关键点, 手动管理 Router
  // 将结果通过 Completer 转发出去
  _dialogStates[tag] ??= [];
  _dialogStates[tag]?.add(safeDialogRoute);
  Future<T?> future = Navigator.of(context, rootNavigator: useRootNavigator)
      .push<T>(safeDialogRoute);
  future.then((result) {
    _dialogStates[tag]?.remove(safeDialogRoute);
    if (!safeDialogRoute.completer.isCompleted) {
      safeDialogRoute.completer.complete(result);
    }
  });
  return safeDialogRoute.completer.future;
}