-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwill_pop_scope.dart
55 lines (50 loc) · 1.67 KB
/
will_pop_scope.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import 'package:flutter/material.dart';
class WillPopScopeExample extends StatefulWidget {
const WillPopScopeExample({super.key});
@override
State<WillPopScopeExample> createState() => _WillPopScopeExampleState();
}
class _WillPopScopeExampleState extends State<WillPopScopeExample> {
Future<bool?> _askConfirmation(BuildContext context) async {
return showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Are you sure?'),
content: const Text('Do you want to go back?'),
actions: [
TextButton(
// User clicks on No then we return false
onPressed: () => Navigator.of(context).pop(false),
child: const Text('No'),
),
TextButton(
// User clicks on Yes then we return true
onPressed: () => Navigator.of(context).pop(true),
child: const Text('Yes'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () async {
// Show the dialog and wait for the user to confirm
bool? confirmation = await _askConfirmation(context);
// If the user confirms (that is true) the route will pop
// If the user cancels (that is false) the route will not pop
// If the dialog is dismissed (that is null) we'll use a default value (false)
return confirmation ?? false;
},
child: Scaffold(
appBar: AppBar(
title: const Text('Will Pop Scope Example'),
),
body: const Center(
child: Text('Press back button to see it in action'),
),
),
);
}
}