Unverified Commit e7d63488 authored by Michael Goderbauer's avatar Michael Goderbauer Committed by GitHub

Add arguments for pushing named routes (#27058)

parent f365217e
......@@ -84,22 +84,8 @@ class StocksAppState extends State<StocksApp> {
}
Route<dynamic> _getRoute(RouteSettings settings) {
// Routes, by convention, are split on slashes, like filesystem paths.
final List<String> path = settings.name.split('/');
// We only support paths that start with a slash, so bail if
// the first component is not empty:
if (path[0] != '')
return null;
// If the path is "/stock:..." then show a stock page for the
// specified stock symbol.
if (path[1].startsWith('stock:')) {
// We don't yet support subpages of a stock, so bail if there's
// any more path components.
if (path.length != 2)
return null;
// Extract the symbol part of "stock:..." and return a route
// for that symbol.
final String symbol = path[1].substring(6);
if (settings.name == '/stocks') {
final String symbol = settings.arguments;
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) => StockSymbolPage(symbol: symbol, stocks: stocks),
......
......@@ -262,7 +262,7 @@ class StockHomeState extends State<StockHome> {
stocks: stocks.toList(),
onAction: _buyStock,
onOpen: (Stock stock) {
Navigator.pushNamed(context, '/stock:${stock.symbol}');
Navigator.pushNamed(context, '/stock', arguments: stock.symbol);
},
onShow: (Stock stock) {
_scaffoldKey.currentState.showBottomSheet<void>((BuildContext context) => StockSymbolBottomSheet(stock: stock));
......
......@@ -4,6 +4,7 @@
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
......@@ -870,4 +871,149 @@ void main() {
semantics.dispose();
});
testWidgets('arguments for named routes on Navigator', (WidgetTester tester) async {
GlobalKey currentRouteKey;
final List<Object> arguments = <Object>[];
await tester.pumpWidget(MaterialApp(
onGenerateRoute: (RouteSettings settings) {
arguments.add(settings.arguments);
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) => Center(key: currentRouteKey = GlobalKey(), child: Text(settings.name)),
);
},
));
expect(find.text('/'), findsOneWidget);
expect(arguments.single, isNull);
arguments.clear();
Navigator.pushNamed(
currentRouteKey.currentContext,
'/A',
arguments: 'pushNamed',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsOneWidget);
expect(arguments.single, 'pushNamed');
arguments.clear();
Navigator.popAndPushNamed(
currentRouteKey.currentContext,
'/B',
arguments: 'popAndPushNamed',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsNothing);
expect(find.text('/B'), findsOneWidget);
expect(arguments.single, 'popAndPushNamed');
arguments.clear();
Navigator.pushNamedAndRemoveUntil(
currentRouteKey.currentContext,
'/C',
(Route<dynamic> route) => route.isFirst,
arguments: 'pushNamedAndRemoveUntil',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsNothing);
expect(find.text('/B'), findsNothing);
expect(find.text('/C'), findsOneWidget);
expect(arguments.single, 'pushNamedAndRemoveUntil');
arguments.clear();
Navigator.pushReplacementNamed(
currentRouteKey.currentContext,
'/D',
arguments: 'pushReplacementNamed',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsNothing);
expect(find.text('/B'), findsNothing);
expect(find.text('/C'), findsNothing);
expect(find.text('/D'), findsOneWidget);
expect(arguments.single, 'pushReplacementNamed');
arguments.clear();
});
testWidgets('arguments for named routes on NavigatorState', (WidgetTester tester) async {
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
final List<Object> arguments = <Object>[];
await tester.pumpWidget(MaterialApp(
navigatorKey: navigatorKey,
onGenerateRoute: (RouteSettings settings) {
arguments.add(settings.arguments);
return MaterialPageRoute<void>(
settings: settings,
builder: (BuildContext context) => Center(child: Text(settings.name)),
);
},
));
expect(find.text('/'), findsOneWidget);
expect(arguments.single, isNull);
arguments.clear();
navigatorKey.currentState.pushNamed(
'/A',
arguments:'pushNamed',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsOneWidget);
expect(arguments.single, 'pushNamed');
arguments.clear();
navigatorKey.currentState.popAndPushNamed(
'/B',
arguments: 'popAndPushNamed',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsNothing);
expect(find.text('/B'), findsOneWidget);
expect(arguments.single, 'popAndPushNamed');
arguments.clear();
navigatorKey.currentState.pushNamedAndRemoveUntil(
'/C',
(Route<dynamic> route) => route.isFirst,
arguments: 'pushNamedAndRemoveUntil',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsNothing);
expect(find.text('/B'), findsNothing);
expect(find.text('/C'), findsOneWidget);
expect(arguments.single, 'pushNamedAndRemoveUntil');
arguments.clear();
navigatorKey.currentState.pushReplacementNamed(
'/D',
arguments: 'pushReplacementNamed',
);
await tester.pumpAndSettle();
expect(find.text('/'), findsNothing);
expect(find.text('/A'), findsNothing);
expect(find.text('/B'), findsNothing);
expect(find.text('/C'), findsNothing);
expect(find.text('/D'), findsOneWidget);
expect(arguments.single, 'pushReplacementNamed');
arguments.clear();
});
}
......@@ -4,6 +4,7 @@
import 'dart:collection';
import 'package:flutter/foundation.dart';
import 'package:mockito/mockito.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/widgets.dart';
......@@ -114,6 +115,23 @@ void main() {
expect(settings3.isInitialRoute, true);
});
testWidgets('Route settings arguments', (WidgetTester tester) async {
const RouteSettings settings = RouteSettings(name: 'A');
expect(settings.arguments, isNull);
final Object arguments = Object();
final RouteSettings settings2 = RouteSettings(name: 'A', arguments: arguments);
expect(settings2.arguments, same(arguments));
final RouteSettings settings3 = settings2.copyWith();
expect(settings3.arguments, equals(arguments));
final Object arguments2 = Object();
final RouteSettings settings4 = settings2.copyWith(arguments: arguments2);
expect(settings4.arguments, same(arguments2));
expect(settings4.arguments, isNot(same(arguments)));
});
testWidgets('Route management - push, replace, pop', (WidgetTester tester) async {
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
await tester.pumpWidget(
......
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