diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 0bfcaf7709..0d33acebb0 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -225,7 +225,9 @@ abstract class Route extends _RoutePlaceholder { void _updateSettings(RouteSettings newSettings) { if (_settings != newSettings) { _settings = newSettings; - changedInternalState(); + if (_navigator != null) { + changedInternalState(); + } } } @@ -3101,10 +3103,6 @@ class _RouteEntry extends RouteTransitionRecord { void handleAdd({ required NavigatorState navigator, required Route? previousPresent }) { assert(currentState == _RouteLifecycle.add); assert(navigator._debugLocked); - assert(route._navigator == null); - route._navigator = navigator; - route.install(); - assert(route.overlayEntries.isNotEmpty); currentState = _RouteLifecycle.adding; navigator._observedRouteAdditions.add( _NavigatorPushObservation(route, previousPresent), @@ -3229,6 +3227,10 @@ class _RouteEntry extends RouteTransitionRecord { } void didAdd({ required NavigatorState navigator, required bool isNewFirst}) { + assert(route._navigator == null); + route._navigator = navigator; + route.install(); + assert(route.overlayEntries.isNotEmpty); route.didAdd(); currentState = _RouteLifecycle.idle; if (isNewFirst) { @@ -3825,7 +3827,9 @@ class NavigatorState extends State with TickerProviderStateMixin, Res super.didChangeDependencies(); _updateHeroController(HeroControllerScope.maybeOf(context)); for (final _RouteEntry entry in _history) { - entry.route.changedExternalState(); + if (entry.route.navigator == this) { + entry.route.changedExternalState(); + } } } @@ -3945,7 +3949,9 @@ class NavigatorState extends State with TickerProviderStateMixin, Res } for (final _RouteEntry entry in _history) { - entry.route.changedExternalState(); + if (entry.route.navigator == this) { + entry.route.changedExternalState(); + } } } diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 2ca9e154f6..af4e0467d0 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -3133,8 +3133,8 @@ void main() { testWidgets('can push and pop pages using page api', (WidgetTester tester) async { late Animation secondaryAnimationOfRouteOne; late Animation primaryAnimationOfRouteOne; - late Animation secondaryAnimationOfRouteTwo; - late Animation primaryAnimationOfRouteTwo; + Animation? secondaryAnimationOfRouteTwo; + Animation? primaryAnimationOfRouteTwo; late Animation secondaryAnimationOfRouteThree; late Animation primaryAnimationOfRouteThree; final GlobalKey navigator = GlobalKey(); @@ -3208,24 +3208,24 @@ void main() { // won't start until the third page finishes transition. expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteThree.value); expect(primaryAnimationOfRouteOne.status, AnimationStatus.completed); - expect(secondaryAnimationOfRouteTwo.status, AnimationStatus.dismissed); - expect(primaryAnimationOfRouteTwo.status, AnimationStatus.dismissed); + expect(secondaryAnimationOfRouteTwo, isNull); + expect(primaryAnimationOfRouteTwo, isNull); expect(secondaryAnimationOfRouteThree.status, AnimationStatus.dismissed); expect(primaryAnimationOfRouteThree.status, AnimationStatus.forward); await tester.pump(const Duration(milliseconds: 30)); expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteThree.value); expect(primaryAnimationOfRouteOne.status, AnimationStatus.completed); - expect(secondaryAnimationOfRouteTwo.status, AnimationStatus.dismissed); - expect(primaryAnimationOfRouteTwo.status, AnimationStatus.dismissed); + expect(secondaryAnimationOfRouteTwo, isNull); + expect(primaryAnimationOfRouteTwo, isNull); expect(secondaryAnimationOfRouteThree.status, AnimationStatus.dismissed); expect(primaryAnimationOfRouteThree.value, 0.1); await tester.pumpAndSettle(); // After transition finishes, the routes' animations are correctly chained. - expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo.value); + expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo!.value); expect(primaryAnimationOfRouteOne.status, AnimationStatus.completed); - expect(secondaryAnimationOfRouteTwo.value, primaryAnimationOfRouteThree.value); - expect(primaryAnimationOfRouteTwo.status, AnimationStatus.completed); + expect(secondaryAnimationOfRouteTwo!.value, primaryAnimationOfRouteThree.value); + expect(primaryAnimationOfRouteTwo!.status, AnimationStatus.completed); expect(secondaryAnimationOfRouteThree.status, AnimationStatus.dismissed); expect(primaryAnimationOfRouteThree.status, AnimationStatus.completed); expect(find.text('third'), findsOneWidget); @@ -3264,17 +3264,17 @@ void main() { ), ); await tester.pump(const Duration(milliseconds: 30)); - expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo.value); + expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo!.value); expect(primaryAnimationOfRouteOne.status, AnimationStatus.completed); - expect(secondaryAnimationOfRouteTwo.value, primaryAnimationOfRouteThree.value); - expect(primaryAnimationOfRouteTwo.status, AnimationStatus.completed); + expect(secondaryAnimationOfRouteTwo!.value, primaryAnimationOfRouteThree.value); + expect(primaryAnimationOfRouteTwo!.status, AnimationStatus.completed); expect(secondaryAnimationOfRouteThree.status, AnimationStatus.dismissed); expect(primaryAnimationOfRouteThree.value, 0.9); await tester.pumpAndSettle(); - expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo.value); + expect(secondaryAnimationOfRouteOne.value, primaryAnimationOfRouteTwo!.value); expect(primaryAnimationOfRouteOne.status, AnimationStatus.completed); - expect(secondaryAnimationOfRouteTwo.value, primaryAnimationOfRouteThree.value); - expect(primaryAnimationOfRouteTwo.status, AnimationStatus.completed); + expect(secondaryAnimationOfRouteTwo!.value, primaryAnimationOfRouteThree.value); + expect(primaryAnimationOfRouteTwo!.status, AnimationStatus.completed); expect(secondaryAnimationOfRouteThree.status, AnimationStatus.dismissed); expect(primaryAnimationOfRouteThree.status, AnimationStatus.dismissed); }); @@ -3415,6 +3415,66 @@ void main() { expect(find.text('page1'), findsOneWidget); }); + testWidgets("Adds multiple pages won't suddenly jump before top most page finishes pushing", (WidgetTester tester) async { + // Regression Test for https://github.com/flutter/flutter/issues/156033. + List> pages = >[ + MaterialPage( + key: UniqueKey(), + child: const Text('home'), + ), + ]; + final GlobalKey key = GlobalKey(); + Widget buildNavigator() { + return TestDependencies( + child: Navigator( + key: key, + pages: pages, + onDidRemovePage: (Page page) {}, + ), + ); + } + await tester.pumpWidget(buildNavigator()); + expect(find.text('home'), findsOneWidget); + + pages = >[ + ...pages, + MaterialPage( + key: UniqueKey(), + child: const Text('child1'), + ), + MaterialPage( + key: UniqueKey(), + child: const Text('child2'), + ), + ]; + + await tester.pumpWidget(buildNavigator()); + expect(find.text('home'), findsOneWidget); + // child 1 should not be created yet because the top-most route is + // still in transition. + expect(find.text('child1'), findsNothing); + expect(find.text('child2'), findsOneWidget); + + await tester.pump(const Duration(milliseconds: 150)); + expect(find.text('home'), findsOneWidget); + expect(find.text('child1'), findsNothing); + expect(find.text('child2'), findsOneWidget); + + await tester.pumpAndSettle(); + + // Ensure child1 is indeed in the navigator. + key.currentState!.pop(); + await tester.pumpAndSettle(); + expect(find.text('home'), findsNothing); + expect(find.text('child1'), findsOneWidget); + expect(find.text('child2'), findsNothing); + + key.currentState!.pop(); + await tester.pumpAndSettle(); + expect(find.text('home'), findsOneWidget); + expect(find.text('child1'), findsNothing); + }); + testWidgets('can work with pageless route', (WidgetTester tester) async { final GlobalKey navigator = GlobalKey(); List myPages = [