confirm static method

Future<bool> confirm(
  1. BuildContext context, {
  2. required String title,
  3. required String message,
  4. String confirmLabel = 'Confirm',
  5. String cancelLabel = 'Cancel',
  6. Widget? icon,
  7. bool isDestructive = false,
  8. bool barrierDismissible = true,
})

Shows a confirmation dialog and returns true only when confirmed.

Implementation

static Future<bool> confirm(
  BuildContext context, {
  required String title,
  required String message,
  String confirmLabel = 'Confirm',
  String cancelLabel = 'Cancel',
  Widget? icon,
  bool isDestructive = false,
  bool barrierDismissible = true,
}) async {
  final result = await show<bool>(
    context,
    barrierDismissible: barrierDismissible,
    builder: (dialogContext) => AlertDialog(
      icon: icon,
      title: Text(title),
      content: Text(message),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(dialogContext).pop(false),
          child: Text(cancelLabel),
        ),
        FilledButton(
          style: isDestructive
              ? FilledButton.styleFrom(
                  backgroundColor: Theme.of(dialogContext).colorScheme.error,
                  foregroundColor: Theme.of(
                    dialogContext,
                  ).colorScheme.onError,
                )
              : null,
          onPressed: () => Navigator.of(dialogContext).pop(true),
          child: Text(confirmLabel),
        ),
      ],
    ),
  );
  return result ?? false;
}