From 204a8848a609309f1c7f1385ce6ef662a83e0290 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Thu, 25 Jan 2024 10:14:49 -0800 Subject: [PATCH] PopScope example improvements (#142163) Attempting to help users understand how to build a confirmation dialog when exiting a route. --- examples/api/lib/widgets/form/form.1.dart | 38 +++++++++++-------- .../lib/widgets/pop_scope/pop_scope.0.dart | 28 +++++++++----- .../flutter/lib/src/widgets/pop_scope.dart | 4 +- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/examples/api/lib/widgets/form/form.1.dart b/examples/api/lib/widgets/form/form.1.dart index b5e8b6e09c..e008f5aaa4 100644 --- a/examples/api/lib/widgets/form/form.1.dart +++ b/examples/api/lib/widgets/form/form.1.dart @@ -62,8 +62,13 @@ class _SaveableFormState extends State<_SaveableForm> { }); } - Future _showDialog() async { - final bool? shouldDiscard = await showDialog( + /// Shows a dialog and resolves to true when the user has indicated that they + /// want to pop. + /// + /// A return value of null indicates a desire not to pop, such as when the + /// user has dismissed the modal without tapping a button. + Future _showDialog() { + return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( @@ -86,19 +91,13 @@ class _SaveableFormState extends State<_SaveableForm> { ); }, ); - - if (shouldDiscard ?? false) { - // Since this is the root route, quit the app where possible by invoking - // the SystemNavigator. If this wasn't the root route, then - // Navigator.maybePop could be used instead. - // See https://github.com/flutter/flutter/issues/11490 - SystemNavigator.pop(); - } } void _save(String? value) { + final String nextSavedValue = value ?? ''; setState(() { - _savedValue = value ?? ''; + _savedValue = nextSavedValue; + _isDirty = nextSavedValue != _controller.text; }); } @@ -112,11 +111,18 @@ class _SaveableFormState extends State<_SaveableForm> { const SizedBox(height: 20.0), Form( canPop: !_isDirty, - onPopInvoked: (bool didPop) { + onPopInvoked: (bool didPop) async { if (didPop) { return; } - _showDialog(); + final bool shouldPop = await _showDialog() ?? false; + if (shouldPop) { + // Since this is the root route, quit the app where possible by + // invoking the SystemNavigator. If this wasn't the root route, + // then Navigator.maybePop could be used instead. + // See https://github.com/flutter/flutter/issues/11490 + SystemNavigator.pop(); + } }, autovalidateMode: AutovalidateMode.always, child: Column( @@ -146,9 +152,9 @@ class _SaveableFormState extends State<_SaveableForm> { ), ), TextButton( - onPressed: () { - if (_isDirty) { - _showDialog(); + onPressed: () async { + final bool shouldPop = !_isDirty || (await _showDialog() ?? false); + if (!shouldPop) { return; } // Since this is the root route, quit the app where possible by diff --git a/examples/api/lib/widgets/pop_scope/pop_scope.0.dart b/examples/api/lib/widgets/pop_scope/pop_scope.0.dart index 6d144bd088..2400b0905e 100644 --- a/examples/api/lib/widgets/pop_scope/pop_scope.0.dart +++ b/examples/api/lib/widgets/pop_scope/pop_scope.0.dart @@ -62,8 +62,13 @@ class _PageTwo extends StatefulWidget { } class _PageTwoState extends State<_PageTwo> { - void _showBackDialog() { - showDialog( + /// Shows a dialog and resolves to true when the user has indicated that they + /// want to pop. + /// + /// A return value of null indicates a desire not to pop, such as when the + /// user has dismissed the modal without tapping a button. + Future _showBackDialog() { + return showDialog( context: context, builder: (BuildContext context) { return AlertDialog( @@ -78,7 +83,7 @@ class _PageTwoState extends State<_PageTwo> { ), child: const Text('Nevermind'), onPressed: () { - Navigator.pop(context); + Navigator.pop(context, false); }, ), TextButton( @@ -87,8 +92,7 @@ class _PageTwoState extends State<_PageTwo> { ), child: const Text('Leave'), onPressed: () { - Navigator.pop(context); - Navigator.pop(context); + Navigator.pop(context, true); }, ), ], @@ -107,15 +111,21 @@ class _PageTwoState extends State<_PageTwo> { const Text('Page Two'), PopScope( canPop: false, - onPopInvoked: (bool didPop) { + onPopInvoked: (bool didPop) async { if (didPop) { return; } - _showBackDialog(); + final bool shouldPop = await _showBackDialog() ?? false; + if (context.mounted && shouldPop) { + Navigator.pop(context); + } }, child: TextButton( - onPressed: () { - _showBackDialog(); + onPressed: () async { + final bool shouldPop = await _showBackDialog() ?? false; + if (context.mounted && shouldPop) { + Navigator.pop(context); + } }, child: const Text('Go back'), ), diff --git a/packages/flutter/lib/src/widgets/pop_scope.dart b/packages/flutter/lib/src/widgets/pop_scope.dart index b47d83fcdb..31af12adee 100644 --- a/packages/flutter/lib/src/widgets/pop_scope.dart +++ b/packages/flutter/lib/src/widgets/pop_scope.dart @@ -26,8 +26,8 @@ import 'routes.dart'; /// [PopScope], in which case it will be `false`. /// /// {@tool dartpad} -/// This sample demonstrates how to use this widget to handle nested navigation -/// in a bottom navigation bar. +/// This sample demonstrates showing a confirmation dialog before navigating +/// away from a page. /// /// ** See code in examples/api/lib/widgets/pop_scope/pop_scope.0.dart ** /// {@end-tool}