Sub topic 7
Handling the back button with PopScope
This chapter assumes Dart async and await, basic Flutter widgets, BuildContext, and how to run a Flutter app. You will learn how to control back navigation without breaking the system back button or Android predictive back gestures.
Why PopScope matters
Flutter can receive a back request from several sources:
- The Android system back button or gesture.
- The back button in an
AppBar. - A browser back action on the web.
- Code that calls
Navigator.pop.
PopScope lets a route decide whether a pop is allowed. A pop is an attempt to remove the current route from the navigation stack. Use canPop to allow or block the attempt, and use onPopInvokedWithResult to react after Flutter processes it.
The callback receives:
didPop:trueif the route was removed.result: the value returned by the route, if one exists.
| Setting | System back behavior | Callback behavior |
|---|---|---|
| `canPop: true` | The route can close | `didPop` is usually `true` |
| `canPop: false` | The route stays open | The callback receives `didPop: false` |
Confirming before leaving
A common case is a form with unsaved changes. Block the first back attempt, show a confirmation dialog, and close the route only when the user confirms.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: EditScreen()));
}
class EditScreen extends StatelessWidget {
const EditScreen({super.key});
Future<bool> _confirmExit(BuildContext context) async {
final answer = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Discard changes?'),
content: const Text('Your changes have not been saved.'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Keep editing'),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Discard'),
),
],
),
);
return answer ?? false;
}
@override
Widget build(BuildContext context) {
return PopScope<void>(
canPop: false,
onPopInvokedWithResult: (didPop, result) async {
if (didPop) return;
final shouldExit = await _confirmExit(context);
if (!context.mounted || !shouldExit) return;
Navigator.of(context).pop();
},
child: Scaffold(
appBar: AppBar(title: const Text('Edit profile')),
body: const Center(child: TextField()),
),
);
}
}The context.mounted check matters because displaying the dialog is asynchronous. The widget might be removed before the dialog finishes. Calling Navigator with an unmounted context can cause an error.
Understanding the pop flow
The flow has two possible paths:
Common errors
Do not call Navigator.pop immediately inside the callback without checking didPop. When canPop is true, the route has already been removed, so another pop could close the previous screen.
Keep the navigation decision in the route that owns the unsaved state. If a child widget owns the form, pass the state upward or place PopScope around the route content.
Exercise
Why does the confirmation example use canPop: false?
Show answer
It blocks the original back request so the route remains available while the user decides whether to discard the changes.