showGlassDialog<T> function Modals

Future<T?> showGlassDialog<T>({
  1. required BuildContext context,
  2. required WidgetBuilder builder,
  3. bool barrierDismissible = false,
  4. String barrierLabel = 'Dismiss',
  5. Duration transitionDuration = const Duration(milliseconds: 250),
})

Shows builder's widget — a GlassAlert, typically — over a dim, centred, in the navigator's overlay.

Note: The GlassHost has to be above the navigator. A glass surface is captured by the host above it, and a modal is built in the Overlay it is shown in — for a dialog or a sheet the navigator's, for a menu or a popover the nearest one. In a WidgetsApp or a MaterialApp that means builder: (context, child) => GlassHost(child: child!). Built anywhere else the modal finds no host, and says so in debug rather than drawing glass that samples nothing.

The route's content inherits the text style, icon theme and app themes in force at context, as showDialog's does. barrierDismissible lets a tap on the dim close it, and barrierLabel is what a screen reader says of the dim. The returned future completes with the value the route is popped with.

final bool? delete = await showGlassDialog<bool>(
  context: context,
  builder: (BuildContext context) => GlassAlert(
    title: const Text('Delete all notes?'),
    message: const Text('This cannot be undone.'),
    actions: <GlassAlertAction>[
      GlassAlertAction(label: 'Cancel', isDefault: true, onPressed: () => Navigator.pop(context, false)),
      GlassAlertAction(label: 'Delete', isDestructive: true, onPressed: () => Navigator.pop(context, true)),
    ],
  ),
);

See also:

Implementation

Future<T?> showGlassDialog<T>({
  required BuildContext context,
  required WidgetBuilder builder,
  bool barrierDismissible = false,
  String barrierLabel = 'Dismiss',
  Duration transitionDuration = const Duration(milliseconds: 250),
}) {
  final NavigatorState navigator = Navigator.of(context);
  final CapturedThemes themes = _capture(context, navigator);
  return navigator.push<T>(
    RawDialogRoute<T>(
      barrierDismissible: barrierDismissible,
      barrierLabel: barrierLabel,
      barrierColor: kGlassModalDim,
      transitionDuration: transitionDuration,
      pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> _) =>
          themes.wrap(SafeArea(child: Center(child: builder(context)))),
    ),
  );
}