diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index f9019614b2..d9fde637a0 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -185,7 +185,6 @@ abstract class TransitionRoute extends OverlayRoute { } break; } - changedInternalState(); } @override @@ -193,16 +192,19 @@ abstract class TransitionRoute extends OverlayRoute { assert(!_transitionCompleter.isCompleted, 'Cannot install a $runtimeType after disposing it.'); _controller = createAnimationController(); assert(_controller != null, '$runtimeType.createAnimationController() returned null.'); - _animation = createAnimation(); + _animation = createAnimation() + ..addStatusListener(_handleStatusChanged); assert(_animation != null, '$runtimeType.createAnimation() returned null.'); super.install(); + if (_animation.isCompleted && overlayEntries.isNotEmpty) { + overlayEntries.first.opaque = opaque; + } } @override TickerFuture didPush() { assert(_controller != null, '$runtimeType.didPush called before calling install() or after calling dispose().'); assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); - _didPushOrReplace(); super.didPush(); return _controller.forward(); } @@ -211,7 +213,6 @@ abstract class TransitionRoute extends OverlayRoute { void didAdd() { assert(_controller != null, '$runtimeType.didPush called before calling install() or after calling dispose().'); assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); - _didPushOrReplace(); super.didAdd(); _controller.value = _controller.upperBound; } @@ -222,19 +223,9 @@ abstract class TransitionRoute extends OverlayRoute { assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); if (oldRoute is TransitionRoute) _controller.value = oldRoute._controller.value; - _didPushOrReplace(); super.didReplace(oldRoute); } - void _didPushOrReplace() { - _animation.addStatusListener(_handleStatusChanged); - // If the animation is already completed, _handleStatusChanged will not get - // a chance to set opaqueness of OverlayEntry. - if (_animation.isCompleted && overlayEntries.isNotEmpty) { - overlayEntries.first.opaque = opaque; - } - } - @override bool didPop(T result) { assert(_controller != null, '$runtimeType.didPop called before calling install() or after calling dispose().'); @@ -856,7 +847,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute of(BuildContext context) { final _ModalScopeStatus widget = context.dependOnInheritedWidgetOfExactType<_ModalScopeStatus>(); @@ -936,7 +927,8 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute !isFirst || willHandlePopInternally; // Internals diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 16fb138a62..9fad17798a 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -1182,7 +1182,50 @@ void main() { expect(log, ['building B', 'building C', 'found C', 'building D']); key.currentState.pop(); await tester.pumpAndSettle(const Duration(milliseconds: 10)); - expect(log, ['building B', 'building C', 'found C', 'building D', 'building C', 'found C']); + expect(log, ['building B', 'building C', 'found C', 'building D']); + }); + + testWidgets('Routes don\'t rebuild just because their animations ended', (WidgetTester tester) async { + final GlobalKey key = GlobalKey(); + final List log = []; + Route nextRoute = PageRouteBuilder( + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { + log.add('building page 1 - ${ModalRoute.of(context).canPop}'); + return const Placeholder(); + }, + ); + await tester.pumpWidget(MaterialApp( + navigatorKey: key, + onGenerateRoute: (RouteSettings settings) { + assert(nextRoute != null); + final Route result = nextRoute; + nextRoute = null; + return result; + }, + )); + expect(log, ['building page 1 - false']); + key.currentState.pushReplacement(PageRouteBuilder( + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { + log.add('building page 2 - ${ModalRoute.of(context).canPop}'); + return const Placeholder(); + }, + )); + expect(log, ['building page 1 - false']); + await tester.pump(); + expect(log, ['building page 1 - false', 'building page 2 - false']); + await tester.pump(const Duration(milliseconds: 150)); + expect(log, ['building page 1 - false', 'building page 2 - false']); + key.currentState.pushReplacement(PageRouteBuilder( + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { + log.add('building page 3 - ${ModalRoute.of(context).canPop}'); + return const Placeholder(); + }, + )); + expect(log, ['building page 1 - false', 'building page 2 - false']); + await tester.pump(); + expect(log, ['building page 1 - false', 'building page 2 - false', 'building page 3 - false']); + await tester.pump(const Duration(milliseconds: 200)); + expect(log, ['building page 1 - false', 'building page 2 - false', 'building page 3 - false']); }); testWidgets('route semantics', (WidgetTester tester) async {