@@ -62,8 +62,13 @@ class _SaveableFormState extends State<_SaveableForm> {
6262 });
6363 }
6464
65- Future <void > _showDialog () async {
66- final bool ? shouldDiscard = await showDialog <bool >(
65+ /// Shows a dialog and resolves to true when the user has indicated that they
66+ /// want to pop.
67+ ///
68+ /// A return value of null indicates a desire not to pop, such as when the
69+ /// user has dismissed the modal without tapping a button.
70+ Future <bool ?> _showDialog () {
71+ return showDialog <bool >(
6772 context: context,
6873 builder: (BuildContext context) {
6974 return AlertDialog (
@@ -86,19 +91,13 @@ class _SaveableFormState extends State<_SaveableForm> {
8691 );
8792 },
8893 );
89-
90- if (shouldDiscard ?? false ) {
91- // Since this is the root route, quit the app where possible by invoking
92- // the SystemNavigator. If this wasn't the root route, then
93- // Navigator.maybePop could be used instead.
94- // See https://github.com/flutter/flutter/issues/11490
95- SystemNavigator .pop ();
96- }
9794 }
9895
9996 void _save (String ? value) {
97+ final String nextSavedValue = value ?? '' ;
10098 setState (() {
101- _savedValue = value ?? '' ;
99+ _savedValue = nextSavedValue;
100+ _isDirty = nextSavedValue != _controller.text;
102101 });
103102 }
104103
@@ -112,11 +111,18 @@ class _SaveableFormState extends State<_SaveableForm> {
112111 const SizedBox (height: 20.0 ),
113112 Form (
114113 canPop: ! _isDirty,
115- onPopInvoked: (bool didPop) {
114+ onPopInvoked: (bool didPop) async {
116115 if (didPop) {
117116 return ;
118117 }
119- _showDialog ();
118+ final bool shouldPop = await _showDialog () ?? false ;
119+ if (shouldPop) {
120+ // Since this is the root route, quit the app where possible by
121+ // invoking the SystemNavigator. If this wasn't the root route,
122+ // then Navigator.maybePop could be used instead.
123+ // See https://github.com/flutter/flutter/issues/11490
124+ SystemNavigator .pop ();
125+ }
120126 },
121127 autovalidateMode: AutovalidateMode .always,
122128 child: Column (
@@ -146,9 +152,9 @@ class _SaveableFormState extends State<_SaveableForm> {
146152 ),
147153 ),
148154 TextButton (
149- onPressed: () {
150- if ( _isDirty) {
151- _showDialog ();
155+ onPressed: () async {
156+ final bool shouldPop = ! _isDirty || ( await _showDialog () ?? false );
157+ if ( ! shouldPop) {
152158 return ;
153159 }
154160 // Since this is the root route, quit the app where possible by
0 commit comments