From 012d2c1e5913aaae55239719734d00b6bb75fb60 Mon Sep 17 00:00:00 2001 From: frmatthew Date: Fri, 27 Mar 2020 15:38:16 -0700 Subject: [PATCH 1/3] Add ability to use rootNavigator --- .../animations/lib/src/open_container.dart | 13 +++- .../animations/test/open_container_test.dart | 70 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/packages/animations/lib/src/open_container.dart b/packages/animations/lib/src/open_container.dart index 92695b007371..f74a5fe5ff8a 100644 --- a/packages/animations/lib/src/open_container.dart +++ b/packages/animations/lib/src/open_container.dart @@ -71,6 +71,7 @@ class OpenContainer extends StatefulWidget { this.tappable = true, this.transitionDuration = const Duration(milliseconds: 300), this.transitionType = ContainerTransitionType.fade, + this.useRootNavigator = false, }) : assert(closedColor != null), assert(openColor != null), assert(closedElevation != null), @@ -81,6 +82,7 @@ class OpenContainer extends StatefulWidget { assert(openBuilder != null), assert(tappable != null), assert(transitionType != null), + assert(useRootNavigator != null), super(key: key); /// Background color of the container while it is closed. @@ -203,6 +205,14 @@ class OpenContainer extends StatefulWidget { /// Defaults to [ContainerTransitionType.fade]. final ContainerTransitionType transitionType; + /// The [useRootNavigator] argument is used to determine whether to push the + /// route for [openBuilder] to the Navigator furthest from or nearest to + /// the given context. + /// + /// By default, [useRootNavigator] is false and the route created will push + /// to the nearest navigator. + final bool useRootNavigator; + @override _OpenContainerState createState() => _OpenContainerState(); } @@ -221,7 +231,8 @@ class _OpenContainerState extends State { final GlobalKey _closedBuilderKey = GlobalKey(); void openContainer() { - Navigator.of(context).push(_OpenContainerRoute( + Navigator.of(context, rootNavigator: widget.useRootNavigator) + .push(_OpenContainerRoute( closedColor: widget.closedColor, openColor: widget.openColor, closedElevation: widget.closedElevation, diff --git a/packages/animations/test/open_container_test.dart b/packages/animations/test/open_container_test.dart index 105988650da3..8d310acdd4eb 100644 --- a/packages/animations/test/open_container_test.dart +++ b/packages/animations/test/open_container_test.dart @@ -1110,6 +1110,7 @@ void main() { ), ), )); + await tester.tap(find.text('Closed')); await tester.pumpAndSettle(); @@ -1448,6 +1449,75 @@ void main() { expect(find.text('Closed 2'), findsNothing); expect(find.text('Open 2'), findsOneWidget); }); + + Widget _createRootNavigatorTest({ + @required Key appKey, + @required Key nestedNavigatorKey, + @required bool useRootNavigator, + }) => + MaterialApp( + key: appKey, + // a nested navigator + home: Navigator( + key: nestedNavigatorKey, + onGenerateRoute: (RouteSettings route) => + MaterialPageRoute( + settings: route, + builder: (BuildContext context) => Container( + child: OpenContainer( + useRootNavigator: useRootNavigator, + closedBuilder: (BuildContext context, _) => + const Text('Closed'), + openBuilder: (BuildContext context, _) => + const Text('Opened'))))), + ); + + testWidgets( + 'Verify that "useRootNavigator: false" uses the correct navigator', + (WidgetTester tester) async { + const Key appKey = Key('App'); + const Key nestedNavigatorKey = Key('Nested Navigator'); + + await tester.pumpWidget(_createRootNavigatorTest( + appKey: appKey, + nestedNavigatorKey: nestedNavigatorKey, + useRootNavigator: false)); + + await tester.tap(find.text('Closed')); + await tester.pumpAndSettle(); + + expect( + find.descendant(of: find.byKey(appKey), matching: find.text('Opened')), + findsOneWidget); + + expect( + find.descendant( + of: find.byKey(nestedNavigatorKey), matching: find.text('Opened')), + findsOneWidget); + }); + + testWidgets('Verify that "useRootNavigator: true" uses the correct navigator', + (WidgetTester tester) async { + const Key appKey = Key('App'); + const Key nestedNavigatorKey = Key('Nested Navigator'); + + await tester.pumpWidget(_createRootNavigatorTest( + appKey: appKey, + nestedNavigatorKey: nestedNavigatorKey, + useRootNavigator: true)); + + await tester.tap(find.text('Closed')); + await tester.pumpAndSettle(); + + expect( + find.descendant(of: find.byKey(appKey), matching: find.text('Opened')), + findsOneWidget); + + expect( + find.descendant( + of: find.byKey(nestedNavigatorKey), matching: find.text('Opened')), + findsNothing); + }); } Color _getScrimColor(WidgetTester tester) { From 7a642db6603b773cb0865b59b7d405c14e850545 Mon Sep 17 00:00:00 2001 From: frmatthew Date: Wed, 22 Apr 2020 15:50:10 -0700 Subject: [PATCH 2/3] Incorporating formatting recommendations --- .../animations/test/open_container_test.dart | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/animations/test/open_container_test.dart b/packages/animations/test/open_container_test.dart index 8d310acdd4eb..baf07b048409 100644 --- a/packages/animations/test/open_container_test.dart +++ b/packages/animations/test/open_container_test.dart @@ -1454,23 +1454,28 @@ void main() { @required Key appKey, @required Key nestedNavigatorKey, @required bool useRootNavigator, - }) => - MaterialApp( + }) { + return MaterialApp( key: appKey, // a nested navigator home: Navigator( key: nestedNavigatorKey, - onGenerateRoute: (RouteSettings route) => - MaterialPageRoute( - settings: route, - builder: (BuildContext context) => Container( + onGenerateRoute: (RouteSettings route) { + return MaterialPageRoute( + settings: route, + builder: (BuildContext context) { + return Container( child: OpenContainer( useRootNavigator: useRootNavigator, - closedBuilder: (BuildContext context, _) => - const Text('Closed'), - openBuilder: (BuildContext context, _) => - const Text('Opened'))))), - ); + closedBuilder: (BuildContext context, _) { + return const Text('Closed'); + }, + openBuilder: (BuildContext context, _) { + return const Text('Opened'); + })); + }); + })); + } testWidgets( 'Verify that "useRootNavigator: false" uses the correct navigator', From eb825ffd6ac1fc6e6d8a9c2b392950b15ad9f2de Mon Sep 17 00:00:00 2001 From: frmatthew Date: Wed, 22 Apr 2020 16:20:54 -0700 Subject: [PATCH 3/3] Final format --- packages/animations/test/open_container_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/animations/test/open_container_test.dart b/packages/animations/test/open_container_test.dart index 91d9267026ce..74f9bdaffedc 100644 --- a/packages/animations/test/open_container_test.dart +++ b/packages/animations/test/open_container_test.dart @@ -1492,7 +1492,7 @@ void main() { expect(find.text('Closed'), findsOneWidget); expect(hasClosed, isTrue); }); - + Widget _createRootNavigatorTest({ @required Key appKey, @required Key nestedNavigatorKey,