From 3000c8bb5922962fb436d152d6ebe5a01e0172ca Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 4 Apr 2017 16:42:31 -0700 Subject: [PATCH] Distinguish canceling a route pop from bubbling the pop up to the next level (#9165) --- .../demo/material/text_form_field_demo.dart | 10 +++--- .../flutter/lib/src/widgets/navigator.dart | 11 +++++-- packages/flutter/lib/src/widgets/routes.dart | 32 ++++++++++++++++--- .../flutter/test/material/will_pop_test.dart | 6 ++++ 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart index 97afe67cdd..2b0e5cd842 100644 --- a/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart +++ b/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart @@ -75,12 +75,12 @@ class TextFormFieldDemoState extends State { return null; } - Future _warnUserAboutInvalidData() { + Future _warnUserAboutInvalidData() async { final FormState form = _formKey.currentState; - if (!_formWasEdited || form.validate()) - return new Future.value(true); + if (form == null || !_formWasEdited || form.validate()) + return true; - return showDialog( + return await showDialog( context: context, child: new AlertDialog( title: new Text('This form has errors'), @@ -96,7 +96,7 @@ class TextFormFieldDemoState extends State { ), ], ), - ); + ) ?? false; } @override diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 1db776f43a..a8f897d2cc 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -13,6 +13,7 @@ import 'focus_manager.dart'; import 'focus_scope.dart'; import 'framework.dart'; import 'overlay.dart'; +import 'routes.dart'; import 'ticker_provider.dart'; /// An abstraction for an entry managed by a [Navigator]. @@ -72,7 +73,9 @@ abstract class Route { /// See also: /// /// * [Form], which provides an `onWillPop` callback that uses this mechanism. - Future willPop() async => !isFirst; + Future willPop() async { + return isFirst ? RoutePopDisposition.bubble : RoutePopDisposition.pop; + } /// A request was made to pop this route. If the route can handle it /// internally (e.g. because it has its own stack of internal state) then @@ -969,8 +972,10 @@ class NavigatorState extends State with TickerProviderStateMixin { Future maybePop([dynamic result]) async { final Route route = _history.last; assert(route._navigator == this); - if (await route.willPop() && mounted) { - pop(result); + final RoutePopDisposition disposition = await route.willPop(); + if (disposition != RoutePopDisposition.bubble && mounted) { + if (disposition == RoutePopDisposition.pop) + pop(result); return true; } return false; diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 5d4ccbe6de..b54ca3a753 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -317,8 +317,10 @@ abstract class LocalHistoryRoute extends Route { } @override - Future willPop() async { - return willHandlePopInternally || await super.willPop(); + Future willPop() async { + if (willHandlePopInternally) + return RoutePopDisposition.pop; + return await super.willPop(); } @override @@ -385,6 +387,28 @@ class _ModalScopeStatus extends InheritedWidget { } } +/// Indicates whether the current route should be popped. +/// +/// Used as the return value for [Route.willPop]. +enum RoutePopDisposition { + /// Pop the route. + /// + /// If [Route.willPop] returns [pop] then the back button will actually pop + /// the current route. + pop, + + /// Do not pop the route. + /// + /// If [Route.willPop] returns [doNotPop] then the back button will be ignored. + doNotPop, + + /// Delegate this to the next level of navigation. + /// + /// If [Route.willPop] return [bubble] then the back button will be handled + /// by the [SystemNavigator], which will usually close the application. + bubble, +} + /// Signature for a callback that verifies that it's OK to call [Navigator.pop]. /// /// Used by [Form.onWillPop], [ModalRoute.addScopedWillPopCallback], and @@ -664,12 +688,12 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute willPop() async { + Future willPop() async { final _ModalScopeState scope = _scopeKey.currentState; assert(scope != null); for (WillPopCallback callback in new List.from(scope._willPopCallbacks)) { if (!await callback()) - return false; + return RoutePopDisposition.doNotPop; } return await super.willPop(); } diff --git a/packages/flutter/test/material/will_pop_test.dart b/packages/flutter/test/material/will_pop_test.dart index 7493f418b8..f63e1996cd 100644 --- a/packages/flutter/test/material/will_pop_test.dart +++ b/packages/flutter/test/material/will_pop_test.dart @@ -113,6 +113,12 @@ void main() { await tester.pump(const Duration(seconds: 1)); expect(find.text('Sample Page'), findsOneWidget); + // Use didPopRoute() to simulate the system back button. Check that + // didPopRoute() indicates that the notification was handled. + final dynamic widgetsAppState = tester.state(find.byType(WidgetsApp)); + expect(await widgetsAppState.didPopRoute(), isTrue); + expect(find.text('Sample Page'), findsOneWidget); + willPopValue = true; await tester.tap(find.byTooltip('Back')); await tester.pump();