Unverified Commit e8b87203 authored by Hans Muller's avatar Hans Muller Committed by GitHub

Handle Cupertino back gesture interrupted by Navigator push (#28756)

parent a1aea2e4
...@@ -272,8 +272,8 @@ class CupertinoPageRoute<T> extends PageRoute<T> { ...@@ -272,8 +272,8 @@ class CupertinoPageRoute<T> extends PageRoute<T> {
_CupertinoBackGestureController<T> backController; _CupertinoBackGestureController<T> backController;
backController = _CupertinoBackGestureController<T>( backController = _CupertinoBackGestureController<T>(
navigator: route.navigator, route: route,
controller: route.controller, controller: route.controller, // protected access
onEnded: () { onEnded: () {
backController?.dispose(); backController?.dispose();
backController = null; backController = null;
...@@ -576,22 +576,15 @@ class _CupertinoBackGestureController<T> { ...@@ -576,22 +576,15 @@ class _CupertinoBackGestureController<T> {
/// ///
/// The [navigator] and [controller] arguments must not be null. /// The [navigator] and [controller] arguments must not be null.
_CupertinoBackGestureController({ _CupertinoBackGestureController({
@required this.navigator, @required this.route,
@required this.controller, @required this.controller,
@required this.onEnded, @required this.onEnded,
}) : assert(navigator != null), }) : assert(route != null), assert(controller != null), assert(onEnded != null) {
assert(controller != null), route.navigator.didStartUserGesture();
assert(onEnded != null) {
navigator.didStartUserGesture();
} }
/// The navigator that this object is controlling. final PageRoute<T> route;
final NavigatorState navigator;
/// The animation controller that the route uses to drive its transition
/// animation.
final AnimationController controller; final AnimationController controller;
final VoidCallback onEnded; final VoidCallback onEnded;
bool _animating = false; bool _animating = false;
...@@ -626,8 +619,10 @@ class _CupertinoBackGestureController<T> { ...@@ -626,8 +619,10 @@ class _CupertinoBackGestureController<T> {
// The closer the panel is to dismissing, the shorter the animation is. // The closer the panel is to dismissing, the shorter the animation is.
// We want to cap the animation time, but we want to use a linear curve // We want to cap the animation time, but we want to use a linear curve
// to determine it. // to determine it.
final int droppedPageForwardAnimationTime = min(lerpDouble(_kMaxDroppedSwipePageForwardAnimationTime, 0, controller.value).floor(), final int droppedPageForwardAnimationTime = min(
_kMaxPageBackAnimationTime); lerpDouble(_kMaxDroppedSwipePageForwardAnimationTime, 0, controller.value).floor(),
_kMaxPageBackAnimationTime,
);
controller.animateTo(1.0, duration: Duration(milliseconds: droppedPageForwardAnimationTime), curve: animationCurve); controller.animateTo(1.0, duration: Duration(milliseconds: droppedPageForwardAnimationTime), curve: animationCurve);
} else { } else {
final int droppedPageBackAnimationTime = lerpDouble(0, _kMaxDroppedSwipePageForwardAnimationTime, controller.value).floor(); final int droppedPageBackAnimationTime = lerpDouble(0, _kMaxDroppedSwipePageForwardAnimationTime, controller.value).floor();
...@@ -652,14 +647,14 @@ class _CupertinoBackGestureController<T> { ...@@ -652,14 +647,14 @@ class _CupertinoBackGestureController<T> {
} }
_animating = false; _animating = false;
if (status == AnimationStatus.dismissed) if (status == AnimationStatus.dismissed)
navigator.pop<T>(); // this will cause the route to get disposed, which will dispose us route.navigator.removeRoute(route); // this will cause the route to get disposed, which will dispose us
onEnded(); // this will call dispose if popping the route failed to do so onEnded(); // this will call dispose if popping the route failed to do so
} }
void dispose() { void dispose() {
if (_animating) if (_animating)
controller.removeStatusListener(_handleStatusChanged); controller.removeStatusListener(_handleStatusChanged);
navigator.didStopUserGesture(); route.navigator?.didStopUserGesture();
} }
} }
......
...@@ -151,11 +151,11 @@ abstract class TransitionRoute<T> extends OverlayRoute<T> { ...@@ -151,11 +151,11 @@ abstract class TransitionRoute<T> extends OverlayRoute<T> {
overlayEntries.first.opaque = false; overlayEntries.first.opaque = false;
break; break;
case AnimationStatus.dismissed: case AnimationStatus.dismissed:
// We might still be the current route if a subclass is controlling the // We might still be an active route if a subclass is controlling the
// the transition and hits the dismissed status. For example, the iOS // the transition and hits the dismissed status. For example, the iOS
// back gesture drives this animation to the dismissed status before // back gesture drives this animation to the dismissed status before
// popping the navigator. // removing the route and disposing it.
if (!isCurrent) { if (!isActive) {
navigator.finalizeRoute(this); navigator.finalizeRoute(this);
assert(overlayEntries.isEmpty); assert(overlayEntries.isEmpty);
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart'; import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
...@@ -253,4 +254,79 @@ void main() { ...@@ -253,4 +254,79 @@ void main() {
expect(find.widgetWithText(CupertinoButton, 'Back'), findsOneWidget); expect(find.widgetWithText(CupertinoButton, 'Back'), findsOneWidget);
expect(tester.getTopLeft(find.text('Back')).dx, 8.0 + 34.0 + 6.0); expect(tester.getTopLeft(find.text('Back')).dx, 8.0 + 34.0 + 6.0);
}); });
testWidgets('Back swipe dismiss interrupted by route push', (WidgetTester tester) async {
final GlobalKey scaffoldKey = GlobalKey();
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(platform: TargetPlatform.iOS),
home: Scaffold(
key: scaffoldKey,
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.push<void>(scaffoldKey.currentContext, MaterialPageRoute<void>(
builder: (BuildContext context) {
return const Scaffold(
body: Center(child: Text('route')),
);
},
));
},
child: const Text('push'),
),
),
),
),
);
// Check the basic iOS back-swipe dismiss transition. Dragging the pushed
// route halfway across the screen will trigger the iOS dismiss animation
await tester.tap(find.text('push'));
await tester.pumpAndSettle();
expect(find.text('route'), findsOneWidget);
expect(find.text('push'), findsNothing);
TestGesture gesture = await tester.startGesture(const Offset(5, 300));
await gesture.moveBy(const Offset(400, 0));
await gesture.up();
await tester.pump();
expect( // The 'route' route has been dragged to the right, halfway across the screen
tester.getTopLeft(find.ancestor(of: find.text('route'), matching: find.byType(Scaffold))),
const Offset(400, 0),
);
expect( // The 'push' route is sliding in from the left.
tester.getTopLeft(find.ancestor(of: find.text('push'), matching: find.byType(Scaffold))).dx,
lessThan(0),
);
await tester.pumpAndSettle();
expect(find.text('push'), findsOneWidget);
expect(
tester.getTopLeft(find.ancestor(of: find.text('push'), matching: find.byType(Scaffold))),
Offset.zero,
);
expect(find.text('route'), findsNothing);
// Run the dismiss animation 75%, which exposes the route "push" button,
// and then press the button. MaterialPageTransition duration is 300ms,
// 275 = 300 * 0.75.
await tester.tap(find.text('push'));
await tester.pumpAndSettle();
expect(find.text('route'), findsOneWidget);
expect(find.text('push'), findsNothing);
gesture = await tester.startGesture(const Offset(5, 300));
await gesture.moveBy(const Offset(400, 0)); // drag halfway
await gesture.up();
await tester.pump(const Duration(milliseconds: 275)); // partially dismiss "route"
expect(find.text('route'), findsOneWidget);
await tester.tap(find.text('push'));
await tester.pumpAndSettle();
expect(find.text('route'), findsOneWidget);
expect(find.text('push'), findsNothing);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment