Check for initialRoute before Navigator.defaultRouteName (#10216)
* Check for initialRoute before Navigator.defaultRouteName * Default initialRoute to Navigator.defaultRouteName * Take suggestions from code review * Add test for old and new routes behavior * Revert "Add test for old and new routes behavior" This reverts commit 282fb64b165ed532583e9a5d2e4debe29469fba4. * Retry: without dartfmt, with dartanalyzer * Rename tests, check the routes are taken * Fix flutter analyze --flutter-repo warnings * Add test for initial vs default route * Update test and fix analyzer warnings * Add test for initial route only being used initially
This commit is contained in:
parent
46b316c490
commit
0cef0aaf35
@ -42,8 +42,9 @@ class MaterialApp extends StatefulWidget {
|
||||
/// Creates a MaterialApp.
|
||||
///
|
||||
/// At least one of [home], [routes], or [onGenerateRoute] must be
|
||||
/// given. If only [routes] is given, it must include an entry for
|
||||
/// the [Navigator.defaultRouteName] (`'/'`).
|
||||
/// given. If only [routes] is given, it must include an entry for the
|
||||
/// [initialRoute], which defaults to [Navigator.defaultRouteName]
|
||||
/// (`'/'`).
|
||||
///
|
||||
/// This class creates an instance of [WidgetsApp].
|
||||
MaterialApp({
|
||||
@ -53,7 +54,7 @@ class MaterialApp extends StatefulWidget {
|
||||
this.theme,
|
||||
this.home,
|
||||
this.routes: const <String, WidgetBuilder>{},
|
||||
this.initialRoute,
|
||||
this.initialRoute: Navigator.defaultRouteName,
|
||||
this.onGenerateRoute,
|
||||
this.onLocaleChanged,
|
||||
this.navigatorObservers: const <NavigatorObserver>[],
|
||||
@ -65,8 +66,8 @@ class MaterialApp extends StatefulWidget {
|
||||
this.debugShowCheckedModeBanner: true
|
||||
}) : assert(debugShowMaterialGrid != null),
|
||||
assert(routes != null),
|
||||
assert(!routes.containsKey(Navigator.defaultRouteName) || (home == null)),
|
||||
assert(routes.containsKey(Navigator.defaultRouteName) || (home != null) || (onGenerateRoute != null)),
|
||||
assert(!routes.containsKey(initialRoute) || (home == null)),
|
||||
assert(routes.containsKey(initialRoute) || (home != null) || (onGenerateRoute != null)),
|
||||
super(key: key);
|
||||
|
||||
/// A one-line description of this app for use in the window manager.
|
||||
|
@ -143,4 +143,76 @@ void main() {
|
||||
|
||||
expect(find.text('Home'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Default initialRoute', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(new MaterialApp(routes: <String, WidgetBuilder>{
|
||||
'/': (BuildContext context) => const Text('route "/"'),
|
||||
}));
|
||||
|
||||
expect(find.text('route "/"'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Custom initialRoute only', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
initialRoute: '/a',
|
||||
routes: <String, WidgetBuilder>{
|
||||
'/a': (BuildContext context) => const Text('route "/a"'),
|
||||
},
|
||||
)
|
||||
);
|
||||
|
||||
expect(find.text('route "/a"'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('Custom initialRoute along with Navigator.defaultRouteName', (WidgetTester tester) async {
|
||||
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
|
||||
'/': (BuildContext context) => const Text('route "/"'),
|
||||
'/a': (BuildContext context) => const Text('route "/a"'),
|
||||
'/b': (BuildContext context) => const Text('route "/b"'),
|
||||
};
|
||||
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
initialRoute: '/a',
|
||||
routes: routes,
|
||||
)
|
||||
);
|
||||
expect(find.text('route "/"'), findsNothing);
|
||||
expect(find.text('route "/a"'), findsOneWidget);
|
||||
expect(find.text('route "/b"'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('Make sure initialRoute is only used the first time', (WidgetTester tester) async {
|
||||
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
|
||||
'/': (BuildContext context) => const Text('route "/"'),
|
||||
'/a': (BuildContext context) => const Text('route "/a"'),
|
||||
'/b': (BuildContext context) => const Text('route "/b"'),
|
||||
};
|
||||
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
initialRoute: '/a',
|
||||
routes: routes,
|
||||
)
|
||||
);
|
||||
expect(find.text('route "/"'), findsNothing);
|
||||
expect(find.text('route "/a"'), findsOneWidget);
|
||||
expect(find.text('route "/b"'), findsNothing);
|
||||
|
||||
await tester.pumpWidget(
|
||||
new MaterialApp(
|
||||
initialRoute: '/b',
|
||||
routes: routes,
|
||||
)
|
||||
);
|
||||
expect(find.text('route "/"'), findsNothing);
|
||||
expect(find.text('route "/a"'), findsOneWidget);
|
||||
expect(find.text('route "/b"'), findsNothing);
|
||||
|
||||
await tester.pumpWidget(new MaterialApp(routes: routes));
|
||||
expect(find.text('route "/"'), findsNothing);
|
||||
expect(find.text('route "/a"'), findsOneWidget);
|
||||
expect(find.text('route "/b"'), findsNothing);
|
||||
});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user