Sub topic 6
Alert dialogs and action sheets in Flutter
This chapter assumes you know basic Dart, StatefulWidget, setState, BuildContext, and Navigator. It also assumes you have used MaterialApp and Scaffold. The examples use Cupertino widgets to present decisions and actions with an iOS-style appearance.
Choosing between a dialog and an action sheet
A CupertinoAlertDialog is a focused question or warning. It keeps the user in the current task until they choose an action. Use it for confirmation, errors, and decisions with only a few choices.
A CupertinoActionSheet is a list of actions that slides up from the bottom of the screen. It works well when the user can choose from several related actions, such as sharing, deleting, or selecting a source.
| Widget | Best for | Presentation function |
|---|---|---|
| `CupertinoAlertDialog` | Confirmation or warning | `showCupertinoDialog` |
| `CupertinoActionSheet` | Several available actions | `showCupertinoModalPopup` |
Both functions return a Future. The future completes when the presented route closes. Navigator.pop closes the dialog or sheet and can optionally return a value.
import 'package:flutter/cupertino.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const CupertinoApp(home: HomePage());
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
Future<void> _confirmDelete(BuildContext context) async {
final shouldDelete = await showCupertinoDialog<bool>(
context: context,
builder: (dialogContext) => CupertinoAlertDialog(
title: const Text('Delete note?'),
content: const Text('This action cannot be undone.'),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.pop(dialogContext, false),
child: const Text('Cancel'),
),
CupertinoDialogAction(
isDestructiveAction: true,
onPressed: () => Navigator.pop(dialogContext, true),
child: const Text('Delete'),
),
],
),
);
if (shouldDelete == true && context.mounted) {
// Delete the note here.
}
}
Future<void> _showActions(BuildContext context) {
return showCupertinoModalPopup<void>(
context: context,
builder: (popupContext) => CupertinoActionSheet(
title: const Text('Note actions'),
message: const Text('Choose an action for this note.'),
actions: [
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(popupContext),
child: const Text('Share'),
),
CupertinoActionSheetAction(
isDestructiveAction: true,
onPressed: () => Navigator.pop(popupContext),
child: const Text('Delete'),
),
],
cancelButton: CupertinoActionSheetAction(
onPressed: () => Navigator.pop(popupContext),
child: const Text('Cancel'),
),
),
);
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Notes'),
),
child: SafeArea(
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CupertinoButton.filled(
onPressed: () => _confirmDelete(context),
child: const Text('Delete note'),
),
CupertinoButton(
onPressed: () => _showActions(context),
child: const Text('More actions'),
),
],
),
),
),
);
}
}isDestructiveAction marks an operation such as deletion. The visual treatment warns the user, but it does not provide safety by itself. The action should still ask for confirmation when the operation is irreversible.
The context.mounted check matters after an asynchronous dialog call. The original screen might have been removed before the future completes. Checking that the context is still mounted prevents using a context that no longer belongs to the widget tree.
How the presentation flow works
Common errors
- Calling
showCupertinoDialogwithout an alert dialog in its builder. - Forgetting to close an action sheet from every action callback.
- Using
showCupertinoDialogfor a long list of choices. - Marking every negative action as destructive, which weakens the warning for genuinely irreversible actions.
- Updating state after awaiting a result without checking
context.mounted.
Exercise
Which presentation function should you use for a bottom sheet containing Share, Copy, and Cancel actions?
Show answer
Use showCupertinoModalPopup with a CupertinoActionSheet. Close it with Navigator.pop from each action.