show<T> static method
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,
- 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;
}