From b68c69aa2e37718f6a935ec5930c40563f784d6b Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Thu, 13 Oct 2022 10:52:24 -0700 Subject: [PATCH] pushnamed can handle Object as type (#113242) --- .../flutter/lib/src/widgets/navigator.dart | 12 +-- .../flutter/test/widgets/navigator_test.dart | 80 ++++++++++++------- 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 4312ddb71a..1166b83ca3 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -4073,7 +4073,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res return index < _history.length ? _history[index] : null; } - Route? _routeNamed(String name, { required Object? arguments, bool allowNull = false }) { + Route? _routeNamed(String name, { required Object? arguments, bool allowNull = false }) { assert(!_debugLocked); assert(name != null); if (allowNull && widget.onGenerateRoute == null) { @@ -4096,7 +4096,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res name: name, arguments: arguments, ); - Route? route = widget.onGenerateRoute!(settings) as Route?; + Route? route = widget.onGenerateRoute!(settings) as Route?; if (route == null && !allowNull) { assert(() { if (widget.onUnknownRoute == null) { @@ -4111,7 +4111,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res } return true; }()); - route = widget.onUnknownRoute!(settings) as Route?; + route = widget.onUnknownRoute!(settings) as Route?; assert(() { if (route == null) { throw FlutterError.fromParts([ @@ -4155,7 +4155,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res String routeName, { Object? arguments, }) { - return push(_routeNamed(routeName, arguments: arguments)!); + return push(_routeNamed(routeName, arguments: arguments)!); } /// Push a named route onto the navigator. @@ -4225,7 +4225,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res TO? result, Object? arguments, }) { - return pushReplacement(_routeNamed(routeName, arguments: arguments)!, result: result); + return pushReplacement(_routeNamed(routeName, arguments: arguments)!, result: result); } /// Replace the current route of the navigator by pushing the route named @@ -4362,7 +4362,7 @@ class NavigatorState extends State with TickerProviderStateMixin, Res RoutePredicate predicate, { Object? arguments, }) { - return pushAndRemoveUntil(_routeNamed(newRouteName, arguments: arguments)!, predicate); + return pushAndRemoveUntil(_routeNamed(newRouteName, arguments: arguments)!, predicate); } /// Push the route with the given name onto the navigator, and then remove all diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index e0f538e399..bedf08595f 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -318,36 +318,56 @@ void main() { expect(log, equals(['left'])); }); - testWidgets('Pending gestures are rejected', (WidgetTester tester) async { - final List log = []; - final Map routes = { - '/': (BuildContext context) { - return Row( - children: [ - GestureDetector( - onTap: () { - log.add('left'); - Navigator.pushNamed(context, '/second'); - }, - child: const Text('left'), - ), - GestureDetector( - onTap: () { log.add('right'); }, - child: const Text('right'), - ), - ], - ); - }, - '/second': (BuildContext context) => Container(), - }; - await tester.pumpWidget(MaterialApp(routes: routes)); - final TestGesture gesture = await tester.startGesture(tester.getCenter(find.text('right')), pointer: 23); - expect(log, isEmpty); - await tester.tap(find.text('left'), pointer: 1); - expect(log, equals(['left'])); - await gesture.up(); - expect(log, equals(['left'])); - }); + testWidgets('pushnamed can handle Object as type', (WidgetTester tester) async { + final GlobalKey nav = GlobalKey(); + final Map routes = { + '/': (BuildContext context) => const Text('/'), + '/second': (BuildContext context) => const Text('/second'), + }; + await tester.pumpWidget(MaterialApp(navigatorKey: nav, routes: routes)); + expect(find.text('/'), findsOneWidget); + Error? error; + try { + nav.currentState!.pushNamed('/second'); + } on Error catch(e) { + error = e; + } + expect(error, isNull); + await tester.pumpAndSettle(); + expect(find.text('/'), findsNothing); + expect(find.text('/second'), findsOneWidget); + }); + + testWidgets('Pending gestures are rejected', (WidgetTester tester) async { + final List log = []; + final Map routes = { + '/': (BuildContext context) { + return Row( + children: [ + GestureDetector( + onTap: () { + log.add('left'); + Navigator.pushNamed(context, '/second'); + }, + child: const Text('left'), + ), + GestureDetector( + onTap: () { log.add('right'); }, + child: const Text('right'), + ), + ], + ); + }, + '/second': (BuildContext context) => Container(), + }; + await tester.pumpWidget(MaterialApp(routes: routes)); + final TestGesture gesture = await tester.startGesture(tester.getCenter(find.text('right')), pointer: 23); + expect(log, isEmpty); + await tester.tap(find.text('left'), pointer: 1); + expect(log, equals(['left'])); + await gesture.up(); + expect(log, equals(['left'])); + }); testWidgets('popAndPushNamed', (WidgetTester tester) async { final Map routes = {