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
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(name != null);
if (allowNull && widget.onGenerateRoute == null) {
......@@ -4096,7 +4096,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
name: name,
arguments: arguments,
);
Route<T>? route = widget.onGenerateRoute!(settings) as Route<T>?;
Route<T?>? route = widget.onGenerateRoute!(settings) as Route<T?>?;
if (route == null && !allowNull) {
assert(() {
if (widget.onUnknownRoute == null) {
......@@ -4111,7 +4111,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
}
return true;
}());
route = widget.onUnknownRoute!(settings) as Route<T>?;
route = widget.onUnknownRoute!(settings) as Route<T?>?;
assert(() {
if (route == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[
......@@ -4155,7 +4155,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
String routeName, {
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.
......@@ -4225,7 +4225,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
TO? result,
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
......@@ -4362,7 +4362,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin, Res
RoutePredicate predicate, {
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
......
......@@ -318,36 +318,56 @@ void main() {
expect(log, equals(<String>['left']));
});
testWidgets('Pending gestures are rejected', (WidgetTester tester) async {
final List<String> log = <String>[];
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (BuildContext context) {
return Row(
children: <Widget>[
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(<String>['left']));
await gesture.up();
expect(log, equals(<String>['left']));
});
testWidgets('pushnamed can handle Object as type', (WidgetTester tester) async {
final GlobalKey<NavigatorState> nav = GlobalKey<NavigatorState>();
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (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<Object>('/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<String> log = <String>[];
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (BuildContext context) {
return Row(
children: <Widget>[
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(<String>['left']));
await gesture.up();
expect(log, equals(<String>['left']));
});
testWidgets('popAndPushNamed', (WidgetTester tester) async {
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