Unverified Commit b68c69aa authored by chunhtai's avatar chunhtai Committed by GitHub

pushnamed can handle Object as type (#113242)

parent 34995ba9
...@@ -4073,7 +4073,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4073,7 +4073,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
return index < _history.length ? _history[index] : null; return index < _history.length ? _history[index] : null;
} }
Route<T>? _routeNamed<T>(String name, { required Object? arguments, bool allowNull = false }) { Route<T?>? _routeNamed<T>(String name, { required Object? arguments, bool allowNull = false }) {
assert(!_debugLocked); assert(!_debugLocked);
assert(name != null); assert(name != null);
if (allowNull && widget.onGenerateRoute == null) { if (allowNull && widget.onGenerateRoute == null) {
...@@ -4096,7 +4096,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4096,7 +4096,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
name: name, name: name,
arguments: arguments, arguments: arguments,
); );
Route<T>? route = widget.onGenerateRoute!(settings) as Route<T>?; Route<T?>? route = widget.onGenerateRoute!(settings) as Route<T?>?;
if (route == null && !allowNull) { if (route == null && !allowNull) {
assert(() { assert(() {
if (widget.onUnknownRoute == null) { if (widget.onUnknownRoute == null) {
...@@ -4111,7 +4111,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4111,7 +4111,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
} }
return true; return true;
}()); }());
route = widget.onUnknownRoute!(settings) as Route<T>?; route = widget.onUnknownRoute!(settings) as Route<T?>?;
assert(() { assert(() {
if (route == null) { if (route == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError.fromParts(<DiagnosticsNode>[
...@@ -4155,7 +4155,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4155,7 +4155,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
String routeName, { String routeName, {
Object? arguments, Object? arguments,
}) { }) {
return push<T>(_routeNamed<T>(routeName, arguments: arguments)!); return push<T?>(_routeNamed<T>(routeName, arguments: arguments)!);
} }
/// Push a named route onto the navigator. /// Push a named route onto the navigator.
...@@ -4225,7 +4225,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4225,7 +4225,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
TO? result, TO? result,
Object? arguments, Object? arguments,
}) { }) {
return pushReplacement<T, TO>(_routeNamed<T>(routeName, arguments: arguments)!, result: result); return pushReplacement<T?, TO>(_routeNamed<T>(routeName, arguments: arguments)!, result: result);
} }
/// Replace the current route of the navigator by pushing the route named /// Replace the current route of the navigator by pushing the route named
...@@ -4362,7 +4362,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res ...@@ -4362,7 +4362,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
RoutePredicate predicate, { RoutePredicate predicate, {
Object? arguments, Object? arguments,
}) { }) {
return pushAndRemoveUntil<T>(_routeNamed<T>(newRouteName, arguments: arguments)!, predicate); return pushAndRemoveUntil<T?>(_routeNamed<T>(newRouteName, arguments: arguments)!, predicate);
} }
/// Push the route with the given name onto the navigator, and then remove all /// Push the route with the given name onto the navigator, and then remove all
......
...@@ -318,36 +318,56 @@ void main() { ...@@ -318,36 +318,56 @@ void main() {
expect(log, equals(<String>['left'])); expect(log, equals(<String>['left']));
}); });
testWidgets('Pending gestures are rejected', (WidgetTester tester) async { testWidgets('pushnamed can handle Object as type', (WidgetTester tester) async {
final List<String> log = <String>[]; final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{ final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (BuildContext context) { '/': (BuildContext context) => const Text('/'),
return Row( '/second': (BuildContext context) => const Text('/second'),
children: <Widget>[ };
GestureDetector( await tester.pumpWidget(MaterialApp(navigatorKey: nav, routes: routes));
onTap: () { expect(find.text('/'), findsOneWidget);
log.add('left'); Error? error;
Navigator.pushNamed(context, '/second'); try {
}, nav.currentState!.pushNamed<Object>('/second');
child: const Text('left'), } on Error catch(e) {
), error = e;
GestureDetector( }
onTap: () { log.add('right'); }, expect(error, isNull);
child: const Text('right'), await tester.pumpAndSettle();
), expect(find.text('/'), findsNothing);
], expect(find.text('/second'), findsOneWidget);
); });
},
'/second': (BuildContext context) => Container(), testWidgets('Pending gestures are rejected', (WidgetTester tester) async {
}; final List<String> log = <String>[];
await tester.pumpWidget(MaterialApp(routes: routes)); final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
final TestGesture gesture = await tester.startGesture(tester.getCenter(find.text('right')), pointer: 23); '/': (BuildContext context) {
expect(log, isEmpty); return Row(
await tester.tap(find.text('left'), pointer: 1); children: <Widget>[
expect(log, equals(<String>['left'])); GestureDetector(
await gesture.up(); onTap: () {
expect(log, equals(<String>['left'])); 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(<String>['left']));
await gesture.up();
expect(log, equals(<String>['left']));
});
testWidgets('popAndPushNamed', (WidgetTester tester) async { testWidgets('popAndPushNamed', (WidgetTester tester) async {
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{ final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
......
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