From 3280be93714c8633f57a940b57a1e4286a11ac21 Mon Sep 17 00:00:00 2001 From: Justin McCandless Date: Fri, 2 Feb 2024 11:27:20 -0800 Subject: [PATCH] Support navigation during a Cupertino back gesture (#142248) Fixes a bug where programmatically navigating during an iOS back gesture caused the app to enter an unstable state. --- packages/flutter/lib/src/cupertino/route.dart | 38 ++- .../flutter/lib/src/widgets/navigator.dart | 2 +- .../flutter/test/cupertino/route_test.dart | 266 ++++++++++++++++++ 3 files changed, 298 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index 3ae8d7a583..188ac7b863 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -158,7 +158,7 @@ mixin CupertinoRouteTransitionMixin on PageRoute { /// True if an iOS-style back swipe pop gesture is currently underway for [route]. /// - /// This just check the route's [NavigatorState.userGestureInProgress]. + /// This just checks the route's [NavigatorState.userGestureInProgress]. /// /// See also: /// @@ -247,6 +247,8 @@ mixin CupertinoRouteTransitionMixin on PageRoute { return _CupertinoBackGestureController( navigator: route.navigator!, + getIsCurrent: () => route.isCurrent, + getIsActive: () => route.isActive, controller: route.controller!, // protected access ); } @@ -293,6 +295,8 @@ mixin CupertinoRouteTransitionMixin on PageRoute { child: _CupertinoBackGestureDetector( enabledCallback: () => _isPopGestureEnabled(route), onStartPopGesture: () => _startPopGesture(route), + getIsCurrent: () => route.isCurrent, + getIsActive: () => route.isActive, child: child, ), ); @@ -596,6 +600,8 @@ class _CupertinoBackGestureDetector extends StatefulWidget { required this.enabledCallback, required this.onStartPopGesture, required this.child, + required this.getIsActive, + required this.getIsCurrent, }); final Widget child; @@ -604,6 +610,9 @@ class _CupertinoBackGestureDetector extends StatefulWidget { final ValueGetter<_CupertinoBackGestureController> onStartPopGesture; + final ValueGetter getIsActive; + final ValueGetter getIsCurrent; + @override _CupertinoBackGestureDetectorState createState() => _CupertinoBackGestureDetectorState(); } @@ -724,12 +733,16 @@ class _CupertinoBackGestureController { _CupertinoBackGestureController({ required this.navigator, required this.controller, + required this.getIsActive, + required this.getIsCurrent, }) { navigator.didStartUserGesture(); } final AnimationController controller; final NavigatorState navigator; + final ValueGetter getIsActive; + final ValueGetter getIsCurrent; /// The drag gesture has changed by [fractionalDelta]. The total range of the /// drag should be 0.0 to 1.0. @@ -745,12 +758,21 @@ class _CupertinoBackGestureController { // This curve has been determined through rigorously eyeballing native iOS // animations. const Curve animationCurve = Curves.fastLinearToSlowEaseIn; + final bool isCurrent = getIsCurrent(); final bool animateForward; - // If the user releases the page before mid screen with sufficient velocity, - // or after mid screen, we should animate the page out. Otherwise, the page - // should be animated back in. - if (velocity.abs() >= _kMinFlingVelocity) { + if (!isCurrent) { + // If the page has already been navigated away from, then the animation + // direction depends on whether or not it's still in the navigation stack, + // regardless of velocity or drag position. For example, if a route is + // being slowly dragged back by just a few pixels, but then a programmatic + // pop occurs, the route should still be animated off the screen. + // See https://github.com/flutter/flutter/issues/141268. + animateForward = getIsActive(); + } else if (velocity.abs() >= _kMinFlingVelocity) { + // If the user releases the page before mid screen with sufficient velocity, + // or after mid screen, we should animate the page out. Otherwise, the page + // should be animated back in. animateForward = velocity <= 0; } else { animateForward = controller.value > 0.5; @@ -766,8 +788,10 @@ class _CupertinoBackGestureController { ); controller.animateTo(1.0, duration: Duration(milliseconds: droppedPageForwardAnimationTime), curve: animationCurve); } else { - // This route is destined to pop at this point. Reuse navigator's pop. - navigator.pop(); + if (isCurrent) { + // This route is destined to pop at this point. Reuse navigator's pop. + navigator.pop(); + } // The popping may have finished inline if already at the target destination. if (controller.isAnimating) { diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index e6a58beb15..4dc79a3cc4 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -2919,7 +2919,7 @@ class _RouteEntry extends RouteTransitionRecord { initialState == _RouteLifecycle.pushReplace || initialState == _RouteLifecycle.replace, ), - currentState = initialState { + currentState = initialState { // TODO(polina-c): stop duplicating code across disposables // https://github.com/flutter/flutter/issues/137435 if (kFlutterMemoryAllocationsEnabled) { diff --git a/packages/flutter/test/cupertino/route_test.dart b/packages/flutter/test/cupertino/route_test.dart index 1f0659f1aa..d6af7ffb0a 100644 --- a/packages/flutter/test/cupertino/route_test.dart +++ b/packages/flutter/test/cupertino/route_test.dart @@ -377,6 +377,272 @@ void main() { ); }); + testWidgets('Back swipe less than halfway is interrupted by route pop', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/141268 + final GlobalKey scaffoldKey = GlobalKey(); + + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + key: scaffoldKey, + child: Center( + child: Column( + children: [ + const Text('Page 1'), + CupertinoButton( + onPressed: () { + Navigator.push(scaffoldKey.currentContext!, CupertinoPageRoute( + builder: (BuildContext context) { + return const CupertinoPageScaffold( + child: Center(child: Text('Page 2')), + ); + }, + )); + }, + child: const Text('Push Page 2'), + ), + ], + ), + ), + ), + ), + ); + + expect(find.text('Page 1'), findsOneWidget); + expect(find.text('Page 2'), findsNothing); + + await tester.tap(find.text('Push Page 2')); + await tester.pumpAndSettle(); + expect(find.text('Page 1'), findsNothing); + expect(find.text('Page 2'), findsOneWidget); + + // Start a back gesture and move it less than 50% across the screen. + final TestGesture gesture = await tester.startGesture(const Offset(5.0, 300.0)); + await gesture.moveBy(const Offset(100.0, 0.0)); + await tester.pump(); + expect( // The second route has been dragged to the right. + tester.getTopLeft(find.ancestor(of: find.text('Page 2'), matching: find.byType(CupertinoPageScaffold))), + const Offset(100.0, 0.0), + ); + expect( // The first route is sliding in from the left. + tester.getTopLeft(find.ancestor(of: find.text('Page 1'), matching: find.byType(CupertinoPageScaffold))).dx, + lessThan(0), + ); + + // Programmatically pop and observe that Page 2 was popped as if there were + // no back gesture. + Navigator.pop(scaffoldKey.currentContext!); + await tester.pumpAndSettle(); + expect( + tester.getTopLeft(find.ancestor(of: find.text('Page 1'), matching: find.byType(CupertinoPageScaffold))), + Offset.zero, + ); + expect(find.text('Page 2'), findsNothing); + }); + + testWidgets('Back swipe more than halfway is interrupted by route pop', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/141268 + final GlobalKey scaffoldKey = GlobalKey(); + + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + key: scaffoldKey, + child: Center( + child: Column( + children: [ + const Text('Page 1'), + CupertinoButton( + onPressed: () { + Navigator.push(scaffoldKey.currentContext!, CupertinoPageRoute( + builder: (BuildContext context) { + return const CupertinoPageScaffold( + child: Center(child: Text('Page 2')), + ); + }, + )); + }, + child: const Text('Push Page 2'), + ), + ], + ), + ), + ), + ), + ); + + expect(find.text('Page 1'), findsOneWidget); + expect(find.text('Page 2'), findsNothing); + + await tester.tap(find.text('Push Page 2')); + await tester.pumpAndSettle(); + expect(find.text('Page 1'), findsNothing); + expect(find.text('Page 2'), findsOneWidget); + + // Start a back gesture and move it more than 50% across the screen. + final TestGesture gesture = await tester.startGesture(const Offset(5.0, 300.0)); + await gesture.moveBy(const Offset(500.0, 0.0)); + await tester.pump(); + expect( // The second route has been dragged to the right. + tester.getTopLeft(find.ancestor(of: find.text('Page 2'), matching: find.byType(CupertinoPageScaffold))), + const Offset(500.0, 0.0), + ); + expect( // The first route is sliding in from the left. + tester.getTopLeft(find.ancestor(of: find.text('Page 1'), matching: find.byType(CupertinoPageScaffold))).dx, + lessThan(0), + ); + + // Programmatically pop and observe that Page 2 was popped as if there were + // no back gesture. + Navigator.pop(scaffoldKey.currentContext!); + await tester.pumpAndSettle(); + expect( + tester.getTopLeft(find.ancestor(of: find.text('Page 1'), matching: find.byType(CupertinoPageScaffold))), + Offset.zero, + ); + expect(find.text('Page 2'), findsNothing); + }); + + testWidgets('Back swipe less than halfway is interrupted by route push', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/141268 + final GlobalKey scaffoldKey = GlobalKey(); + + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + key: scaffoldKey, + child: Center( + child: Column( + children: [ + const Text('Page 1'), + CupertinoButton( + onPressed: () { + Navigator.push(scaffoldKey.currentContext!, CupertinoPageRoute( + builder: (BuildContext context) { + return const CupertinoPageScaffold( + child: Center(child: Text('Page 2')), + ); + }, + )); + }, + child: const Text('Push Page 2'), + ), + ], + ), + ), + ), + ), + ); + + expect(find.text('Page 1'), findsOneWidget); + expect(find.text('Page 2'), findsNothing); + + await tester.tap(find.text('Push Page 2')); + await tester.pumpAndSettle(); + expect(find.text('Page 1'), findsNothing); + expect(find.text('Page 2'), findsOneWidget); + + // Start a back gesture and move it less than 50% across the screen. + final TestGesture gesture = await tester.startGesture(const Offset(5.0, 300.0)); + await gesture.moveBy(const Offset(100.0, 0.0)); + await tester.pump(); + expect( // The second route has been dragged to the right. + tester.getTopLeft(find.ancestor(of: find.text('Page 2'), matching: find.byType(CupertinoPageScaffold))), + const Offset(100.0, 0.0), + ); + expect( // The first route is sliding in from the left. + tester.getTopLeft(find.ancestor(of: find.text('Page 1'), matching: find.byType(CupertinoPageScaffold))).dx, + lessThan(0), + ); + + // Programmatically push and observe that Page 3 was pushed as if there were + // no back gesture. + Navigator.push(scaffoldKey.currentContext!, CupertinoPageRoute( + builder: (BuildContext context) { + return const CupertinoPageScaffold( + child: Center(child: Text('Page 3')), + ); + }, + )); + await tester.pumpAndSettle(); + expect(find.text('Page 1'), findsNothing); + expect(find.text('Page 2'), findsNothing); + expect( + tester.getTopLeft(find.ancestor(of: find.text('Page 3'), matching: find.byType(CupertinoPageScaffold))), + Offset.zero, + ); + }); + + testWidgets('Back swipe more than halfway is interrupted by route push', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/141268 + final GlobalKey scaffoldKey = GlobalKey(); + + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + key: scaffoldKey, + child: Center( + child: Column( + children: [ + const Text('Page 1'), + CupertinoButton( + onPressed: () { + Navigator.push(scaffoldKey.currentContext!, CupertinoPageRoute( + builder: (BuildContext context) { + return const CupertinoPageScaffold( + child: Center(child: Text('Page 2')), + ); + }, + )); + }, + child: const Text('Push Page 2'), + ), + ], + ), + ), + ), + ), + ); + + expect(find.text('Page 1'), findsOneWidget); + expect(find.text('Page 2'), findsNothing); + + await tester.tap(find.text('Push Page 2')); + await tester.pumpAndSettle(); + expect(find.text('Page 1'), findsNothing); + expect(find.text('Page 2'), findsOneWidget); + + // Start a back gesture and move it more than 50% across the screen. + final TestGesture gesture = await tester.startGesture(const Offset(5.0, 300.0)); + await gesture.moveBy(const Offset(500.0, 0.0)); + await tester.pump(); + expect( // The second route has been dragged to the right. + tester.getTopLeft(find.ancestor(of: find.text('Page 2'), matching: find.byType(CupertinoPageScaffold))), + const Offset(500.0, 0.0), + ); + expect( // The first route is sliding in from the left. + tester.getTopLeft(find.ancestor(of: find.text('Page 1'), matching: find.byType(CupertinoPageScaffold))).dx, + lessThan(0), + ); + + // Programmatically push and observe that Page 3 was pushed as if there were + // no back gesture. + Navigator.push(scaffoldKey.currentContext!, CupertinoPageRoute( + builder: (BuildContext context) { + return const CupertinoPageScaffold( + child: Center(child: Text('Page 3')), + ); + }, + )); + await tester.pumpAndSettle(); + expect(find.text('Page 1'), findsNothing); + expect(find.text('Page 2'), findsNothing); + expect( + tester.getTopLeft(find.ancestor(of: find.text('Page 3'), matching: find.byType(CupertinoPageScaffold))), + Offset.zero, + ); + }); + testWidgets('Fullscreen route animates correct transform values over time', (WidgetTester tester) async { await tester.pumpWidget( CupertinoApp(