showGlassConfirmationDialog<T> function

Future<T?> showGlassConfirmationDialog<T>({
  1. required BuildContext context,
  2. required List<GlassConfirmationDialogAction> actions,
  3. String? title,
  4. String? message,
  5. String? cancelLabel,
  6. bool? showCancelButton,
  7. GlassMorphAnchor? morphFrom,
  8. Rect? morphFromRect,
  9. GlassMenuAlignment? alignment,
  10. MorphSpeed morphSpeed = MorphSpeed.normal,
  11. bool barrierDismissible = true,
  12. Color? barrierColor,
  13. double? maxWidth,
  14. LiquidGlassSettings? settings,
  15. GlassQuality? quality,
  16. bool platformViewBackdrop = false,
  17. LiquidShape? shape,
  18. LiquidShape? actionShape,
  19. TextStyle? actionTextStyle,
  20. bool useRootNavigator = true,
})

Presents a native iOS 26/27 Liquid Glass confirmation dialog, mirroring SwiftUI's .confirmationDialog() and UIKit's UIAlertController(preferredStyle: .actionSheet).

Source-anchored vs. unanchored presentation

  • Source-anchored (morphFrom via GlassMorphTrigger, or explicit morphFromRect): liquid-morphs directly out of the source trigger into a calibrated 240 pt floating Liquid Glass popover platter with a clear dismiss barrier, omitting the Cancel button by default (since tapping outside dismisses), and retracts back into the trigger with catch recoil on dismissal.
  • Unanchored (morphFrom == null && morphFromRect == null): presents the calibrated 320 pt centered Liquid Glass platter with stacked 48 pt capsule actions and a Cancel button included by default at the bottom.

Pass showCancelButton explicitly (true or false) to override the automatic anchored/unanchored Cancel button rule.

Usage

GlassMorphTrigger(
  builder: (context, anchor) => GlassIconButton(
    icon: const Icon(CupertinoIcons.trash),
    onPressed: () => showGlassConfirmationDialog<void>(
      context: context,
      morphFrom: anchor,
      title: 'Delete Draft?',
      message: 'This draft cannot be recovered once deleted.',
      actions: [
        GlassConfirmationDialogAction(
          label: 'Save Draft',
          onPressed: _saveDraft,
        ),
        GlassConfirmationDialogAction(
          label: 'Delete Draft',
          isDestructive: true,
          onPressed: _deleteDraft,
        ),
      ],
    ),
  ),
)

Unanchored (centered platter with Cancel)

await showGlassConfirmationDialog<void>(
  context: context,
  title: 'Choose Action',
  message: 'Pick what to do with this item.',
  actions: [
    GlassConfirmationDialogAction(label: 'Share', onPressed: _share),
    GlassConfirmationDialogAction(
      label: 'Delete',
      isDestructive: true,
      onPressed: _delete,
    ),
  ],
);

Implementation

Future<T?> showGlassConfirmationDialog<T>({
  required BuildContext context,
  required List<GlassConfirmationDialogAction> actions,
  String? title,
  String? message,
  String? cancelLabel,
  bool? showCancelButton,
  GlassMorphAnchor? morphFrom,
  Rect? morphFromRect,
  GlassMenuAlignment? alignment,
  MorphSpeed morphSpeed = MorphSpeed.normal,
  bool barrierDismissible = true,
  Color? barrierColor,
  double? maxWidth,
  LiquidGlassSettings? settings,
  GlassQuality? quality,
  bool platformViewBackdrop = false,
  LiquidShape? shape,
  LiquidShape? actionShape,
  TextStyle? actionTextStyle,
  bool useRootNavigator = true,
}) {
  assert(
    morphFrom == null || morphFromRect == null,
    'Pass either morphFrom (the GlassMorphAnchor from a GlassMorphTrigger) '
    'or morphFromRect (an explicit global rect) — not both.',
  );

  final capturedThemes = CapturedModalThemes.forNavigator(
    context,
    useRootNavigator: useRootNavigator,
  );
  final triggerRect = resolveMorphTriggerRect(
    morphFrom,
    morphFromRect,
    callerName: 'showGlassConfirmationDialog',
  );
  final effectiveShowCancel = showCancelButton ?? (triggerRect == null);
  assert(
    actions.isNotEmpty || effectiveShowCancel,
    'showGlassConfirmationDialog requires at least one action when '
    'showCancelButton is false.',
  );

  final localizations = glassCupertinoLocalizationsOf(context);
  final effectiveCancelLabel = cancelLabel ?? localizations.cancelButtonLabel;

  if (triggerRect != null) {
    final reduceMotion = GlassAccessibilityData.of(context).reduceMotion;

    return Navigator.of(context, rootNavigator: useRootNavigator).push<T>(
      _GlassConfirmationPopoverRoute<T>(
        triggerRect: triggerRect,
        anchor: morphFrom,
        alignment: alignment,
        morphSpeed: morphSpeed,
        reduceMotionAtPush: reduceMotion,
        barrierDismissible: barrierDismissible,
        barrierColorValue: barrierColor,
        barrierLabelValue: localizations.modalBarrierDismissLabel,
        capturedThemes: capturedThemes,
        title: title,
        message: message,
        actions: actions,
        cancelLabel: effectiveCancelLabel,
        showCancelButton: effectiveShowCancel,
        maxWidth: maxWidth,
        glassSettings: settings,
        quality: quality,
        platformViewBackdrop: platformViewBackdrop,
        shape: shape,
        actionShape: actionShape,
        actionTextStyle: actionTextStyle,
      ),
    );
  }

  // Unanchored: calibrated 320 pt centered platter with Cancel by default.
  final isDark = GlassTheme.brightnessOf(context) == Brightness.dark;
  final resolvedBarrierColor = barrierColor != null
      ? CupertinoDynamicColor.resolve(barrierColor, context)
      : glassAlertBarrierColor(isDark: isDark);

  return pushGlassAlertRoute<T>(
    context: context,
    useRootNavigator: useRootNavigator,
    barrierDismissible: barrierDismissible,
    barrierColor: resolvedBarrierColor,
    builder: (modalContext) {
      final route = ModalRoute.of(modalContext);
      void dismissIfActive() {
        if (route != null && route.isCurrent && route.isActive) {
          Navigator.of(modalContext).pop();
        }
      }

      return capturedThemes.wrap(
        GlassAlertPlatter(
          title: title,
          message: message,
          settings: settings,
          quality: quality,
          maxWidth: maxWidth,
          forceStacked: true,
          semanticsRole: SemanticsRole.dialog,
          shape: shape,
          actionShape: actionShape,
          actionTextStyle: actionTextStyle,
          actions: _buildPlatterActions(
            actions: actions,
            showCancelButton: effectiveShowCancel,
            cancelLabel: effectiveCancelLabel,
            dismissIfActive: dismissIfActive,
          ),
        ),
      );
    },
  );
}