A dialog temporarily asks for a focused decision. A bottom sheet can reveal contextual actions or supporting content while keeping the current screen in view. Neither should replace an ordinary page simply to make navigation look different.
Return a decision
The fragment belongs in a callback with a valid BuildContext. The result is nullable because dismissing the dialog is a legitimate path.
Worked example
This is a focused code fragment. Place it in the class or file described above; it is not a complete standalone app.
final confirmed = await showDialog<bool>(context: context,
builder: (dialogContext) => AlertDialog(
title: const Text('Discard this draft?'),
content: const Text('Unsaved changes will be removed.'),
actions: [
TextButton(onPressed: () => Navigator.pop(dialogContext, false),
child: const Text('Keep editing')),
FilledButton(onPressed: () => Navigator.pop(dialogContext, true),
child: const Text('Discard draft')),
],
),
);
if (!context.mounted) return;
if (confirmed == true) {
// Discard the draft through its owner.
}
Read the result
Only an explicit true triggers the destructive operation. The mounted check matters after the asynchronous wait if subsequent work uses this screen context. Keep the data mutation in its actual owner.
Enumerate every way the decision can end
The user can choose the destructive action, choose to keep editing, dismiss the surface or navigate back. The caller should mutate data only after the explicit result that authorizes that mutation. A null return is a normal cancellation path, not necessarily an application error.
The dialog callback uses its own context to close the correct route. The caller then checks whether its context is still mounted after await before showing further interface feedback. The actual draft owner performs the data operation; the dialog only communicates a decision.
This is the same result-flow idea used in returning data from navigation . Test cancellation first, then the happy path, so the feature does not accidentally discard data on a back gesture.
Practice and verification
Test both buttons, a tap outside the dialog and a back gesture. All cancellation paths should preserve the draft. Then compare whether an inline Undo pattern would be more appropriate.