Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
e7d63488
Unverified
Commit
e7d63488
authored
Jan 30, 2019
by
Michael Goderbauer
Committed by
GitHub
Jan 30, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add arguments for pushing named routes (#27058)
parent
f365217e
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
306 additions
and
45 deletions
+306
-45
main.dart
examples/stocks/lib/main.dart
+2
-16
stock_home.dart
examples/stocks/lib/stock_home.dart
+1
-1
navigator.dart
packages/flutter/lib/src/widgets/navigator.dart
+139
-28
navigator_test.dart
packages/flutter/test/widgets/navigator_test.dart
+146
-0
routes_test.dart
packages/flutter/test/widgets/routes_test.dart
+18
-0
No files found.
examples/stocks/lib/main.dart
View file @
e7d63488
...
@@ -84,22 +84,8 @@ class StocksAppState extends State<StocksApp> {
...
@@ -84,22 +84,8 @@ class StocksAppState extends State<StocksApp> {
}
}
Route
<
dynamic
>
_getRoute
(
RouteSettings
settings
)
{
Route
<
dynamic
>
_getRoute
(
RouteSettings
settings
)
{
// Routes, by convention, are split on slashes, like filesystem paths.
if
(
settings
.
name
==
'/stocks'
)
{
final
List
<
String
>
path
=
settings
.
name
.
split
(
'/'
);
final
String
symbol
=
settings
.
arguments
;
// 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
);
return
MaterialPageRoute
<
void
>(
return
MaterialPageRoute
<
void
>(
settings:
settings
,
settings:
settings
,
builder:
(
BuildContext
context
)
=>
StockSymbolPage
(
symbol:
symbol
,
stocks:
stocks
),
builder:
(
BuildContext
context
)
=>
StockSymbolPage
(
symbol:
symbol
,
stocks:
stocks
),
...
...
examples/stocks/lib/stock_home.dart
View file @
e7d63488
...
@@ -262,7 +262,7 @@ class StockHomeState extends State<StockHome> {
...
@@ -262,7 +262,7 @@ class StockHomeState extends State<StockHome> {
stocks:
stocks
.
toList
(),
stocks:
stocks
.
toList
(),
onAction:
_buyStock
,
onAction:
_buyStock
,
onOpen:
(
Stock
stock
)
{
onOpen:
(
Stock
stock
)
{
Navigator
.
pushNamed
(
context
,
'/stock
:
${stock.symbol}
'
);
Navigator
.
pushNamed
(
context
,
'/stock
'
,
arguments:
stock
.
symbol
);
},
},
onShow:
(
Stock
stock
)
{
onShow:
(
Stock
stock
)
{
_scaffoldKey
.
currentState
.
showBottomSheet
<
void
>((
BuildContext
context
)
=>
StockSymbolBottomSheet
(
stock:
stock
));
_scaffoldKey
.
currentState
.
showBottomSheet
<
void
>((
BuildContext
context
)
=>
StockSymbolBottomSheet
(
stock:
stock
));
...
...
packages/flutter/lib/src/widgets/navigator.dart
View file @
e7d63488
...
@@ -289,6 +289,7 @@ class RouteSettings {
...
@@ -289,6 +289,7 @@ class RouteSettings {
const
RouteSettings
({
const
RouteSettings
({
this
.
name
,
this
.
name
,
this
.
isInitialRoute
=
false
,
this
.
isInitialRoute
=
false
,
this
.
arguments
,
});
});
/// Creates a copy of this route settings object with the given fields
/// Creates a copy of this route settings object with the given fields
...
@@ -296,10 +297,12 @@ class RouteSettings {
...
@@ -296,10 +297,12 @@ class RouteSettings {
RouteSettings
copyWith
({
RouteSettings
copyWith
({
String
name
,
String
name
,
bool
isInitialRoute
,
bool
isInitialRoute
,
Object
arguments
,
})
{
})
{
return
RouteSettings
(
return
RouteSettings
(
name:
name
??
this
.
name
,
name:
name
??
this
.
name
,
isInitialRoute:
isInitialRoute
??
this
.
isInitialRoute
,
isInitialRoute:
isInitialRoute
??
this
.
isInitialRoute
,
arguments:
arguments
??
this
.
arguments
,
);
);
}
}
...
@@ -313,8 +316,13 @@ class RouteSettings {
...
@@ -313,8 +316,13 @@ class RouteSettings {
/// The initial route typically skips any entrance transition to speed startup.
/// The initial route typically skips any entrance transition to speed startup.
final
bool
isInitialRoute
;
final
bool
isInitialRoute
;
/// The arguments passed to this route.
///
/// May be used when building the route, e.g. in [Navigator.onGenerateRoute].
final
Object
arguments
;
@override
@override
String
toString
()
=>
'
"
$name
"
'
;
String
toString
()
=>
'
$runtimeType
("
$name
",
$arguments
)
'
;
}
}
/// An interface for observing the behavior of a [Navigator].
/// An interface for observing the behavior of a [Navigator].
...
@@ -725,19 +733,77 @@ class Navigator extends StatefulWidget {
...
@@ -725,19 +733,77 @@ class Navigator extends StatefulWidget {
/// The `T` type argument is the type of the return value of the route.
/// The `T` type argument is the type of the return value of the route.
/// {@endtemplate}
/// {@endtemplate}
///
///
/// {@template flutter.widgets.navigator.pushNamed.arguments}
/// The provided `arguments` are passed to the pushed route via
/// [RouteSettings.arguments]. Any object can be passed as `arguments` (e.g. a
/// [String], [int], or an instance of a custom `MyRouteArguments` class).
/// Often, a [Map] is used to pass key-value pairs.
///
/// The `arguments` may be used in [Navigator.onGenerateRoute] or
/// [Navigator.onUnknownRoute] to construct the route.
/// {@endtemplate}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
///
///
/// ```dart
/// ```dart
/// void _didPushButton() {
/// void _didPushButton() {
/// Navigator.pushNamed(context, '/nyc/1776');
/// Navigator.pushNamed(context, '/settings');
/// }
/// ```
/// {@end-tool}
///
/// {@tool sample}
///
/// The following example shows how to pass additional `arguments` to the
/// route:
///
/// ```dart
/// void _showBerlinWeather() {
/// Navigator.pushNamed(
/// context,
/// '/weather',
/// arguments: <String, String>{
/// 'city': 'Berlin',
/// 'country': 'Germany',
/// },
/// );
/// }
/// ```
/// {@end-tool}
///
/// {@tool sample}
///
/// The following example shows how to pass a custom Object to the route:
///
/// ```dart
/// class WeatherRouteArguments {
/// WeatherRouteArguments({ this.city, this.country });
/// final String city;
/// final String country;
///
/// bool get isGermanCapital {
/// return country == 'Germany' && city == 'Berlin';
/// }
/// }
///
/// void _showWeather() {
/// Navigator.pushNamed(
/// context,
/// '/weather',
/// arguments: WeatherRouteArguments(city: 'Berlin', country: 'Germany'),
/// );
/// }
/// }
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
static
Future
<
T
>
pushNamed
<
T
extends
Object
>(
BuildContext
context
,
String
routeName
)
{
static
Future
<
T
>
pushNamed
<
T
extends
Object
>(
return
Navigator
.
of
(
context
).
pushNamed
<
T
>(
routeName
);
BuildContext
context
,
String
routeName
,
{
Object
arguments
,
})
{
return
Navigator
.
of
(
context
).
pushNamed
<
T
>(
routeName
,
arguments:
arguments
);
}
}
/// Replace the current route of the navigator that most tightly encloses the
/// Replace the current route of the navigator that most tightly encloses the
...
@@ -773,19 +839,26 @@ class Navigator extends StatefulWidget {
...
@@ -773,19 +839,26 @@ class Navigator extends StatefulWidget {
/// and `TO` is the type of the return value of the old route.
/// and `TO` is the type of the return value of the old route.
/// {@endtemplate}
/// {@endtemplate}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
///
///
/// ```dart
/// ```dart
/// void _s
howNext
() {
/// void _s
witchToBrightness
() {
/// Navigator.pushReplacementNamed(context, '/
jouett/1781
');
/// Navigator.pushReplacementNamed(context, '/
settings/brightness
');
/// }
/// }
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
static
Future
<
T
>
pushReplacementNamed
<
T
extends
Object
,
TO
extends
Object
>(
BuildContext
context
,
String
routeName
,
{
TO
result
})
{
static
Future
<
T
>
pushReplacementNamed
<
T
extends
Object
,
TO
extends
Object
>(
return
Navigator
.
of
(
context
).
pushReplacementNamed
<
T
,
TO
>(
routeName
,
result:
result
);
BuildContext
context
,
String
routeName
,
{
TO
result
,
Object
arguments
,
})
{
return
Navigator
.
of
(
context
).
pushReplacementNamed
<
T
,
TO
>(
routeName
,
arguments:
arguments
,
result:
result
);
}
}
/// Pop the current route off the navigator that most tightly encloses the
/// Pop the current route off the navigator that most tightly encloses the
...
@@ -821,19 +894,26 @@ class Navigator extends StatefulWidget {
...
@@ -821,19 +894,26 @@ class Navigator extends StatefulWidget {
/// and `TO` is the return value type of the old route.
/// and `TO` is the return value type of the old route.
/// {@endtemplate}
/// {@endtemplate}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
///
///
/// ```dart
/// ```dart
/// void _select
NewYork
() {
/// void _select
Accessibility
() {
/// Navigator.popAndPushNamed(context, '/
nyc/1776
');
/// Navigator.popAndPushNamed(context, '/
settings/accessibility
');
/// }
/// }
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
static
Future
<
T
>
popAndPushNamed
<
T
extends
Object
,
TO
extends
Object
>(
BuildContext
context
,
String
routeName
,
{
TO
result
})
{
static
Future
<
T
>
popAndPushNamed
<
T
extends
Object
,
TO
extends
Object
>(
return
Navigator
.
of
(
context
).
popAndPushNamed
<
T
,
TO
>(
routeName
,
result:
result
);
BuildContext
context
,
String
routeName
,
{
TO
result
,
Object
arguments
,
})
{
return
Navigator
.
of
(
context
).
popAndPushNamed
<
T
,
TO
>(
routeName
,
arguments:
arguments
,
result:
result
);
}
}
/// Push the route with the given name onto the navigator that most tightly
/// Push the route with the given name onto the navigator that most tightly
...
@@ -875,6 +955,8 @@ class Navigator extends StatefulWidget {
...
@@ -875,6 +955,8 @@ class Navigator extends StatefulWidget {
/// The `T` type argument is the type of the return value of the new route.
/// The `T` type argument is the type of the return value of the new route.
/// {@endtemplate}
/// {@endtemplate}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
...
@@ -886,8 +968,13 @@ class Navigator extends StatefulWidget {
...
@@ -886,8 +968,13 @@ class Navigator extends StatefulWidget {
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
static
Future
<
T
>
pushNamedAndRemoveUntil
<
T
extends
Object
>(
BuildContext
context
,
String
newRouteName
,
RoutePredicate
predicate
)
{
static
Future
<
T
>
pushNamedAndRemoveUntil
<
T
extends
Object
>(
return
Navigator
.
of
(
context
).
pushNamedAndRemoveUntil
<
T
>(
newRouteName
,
predicate
);
BuildContext
context
,
String
newRouteName
,
RoutePredicate
predicate
,
{
Object
arguments
,
})
{
return
Navigator
.
of
(
context
).
pushNamedAndRemoveUntil
<
T
>(
newRouteName
,
predicate
,
arguments:
arguments
);
}
}
/// Push the given route onto the navigator that most tightly encloses the
/// Push the given route onto the navigator that most tightly encloses the
...
@@ -1329,7 +1416,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1329,7 +1416,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
Navigator
.
defaultRouteName
,
Navigator
.
defaultRouteName
,
];
];
final
List
<
Route
<
dynamic
>>
plannedInitialRoutes
=
<
Route
<
dynamic
>>[
final
List
<
Route
<
dynamic
>>
plannedInitialRoutes
=
<
Route
<
dynamic
>>[
_routeNamed
<
dynamic
>(
Navigator
.
defaultRouteName
,
allowNull:
true
),
_routeNamed
<
dynamic
>(
Navigator
.
defaultRouteName
,
allowNull:
true
,
arguments:
null
),
];
];
final
List
<
String
>
routeParts
=
initialRouteName
.
split
(
'/'
);
final
List
<
String
>
routeParts
=
initialRouteName
.
split
(
'/'
);
if
(
initialRouteName
.
isNotEmpty
)
{
if
(
initialRouteName
.
isNotEmpty
)
{
...
@@ -1337,7 +1424,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1337,7 +1424,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
for
(
String
part
in
routeParts
)
{
for
(
String
part
in
routeParts
)
{
routeName
+=
'/
$part
'
;
routeName
+=
'/
$part
'
;
plannedInitialRouteNames
.
add
(
routeName
);
plannedInitialRouteNames
.
add
(
routeName
);
plannedInitialRoutes
.
add
(
_routeNamed
<
dynamic
>(
routeName
,
allowNull:
true
));
plannedInitialRoutes
.
add
(
_routeNamed
<
dynamic
>(
routeName
,
allowNull:
true
,
arguments:
null
));
}
}
}
}
if
(
plannedInitialRoutes
.
contains
(
null
))
{
if
(
plannedInitialRoutes
.
contains
(
null
))
{
...
@@ -1357,15 +1444,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1357,15 +1444,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
);
);
return
true
;
return
true
;
}());
}());
push
(
_routeNamed
<
Object
>(
Navigator
.
defaultRouteName
));
push
(
_routeNamed
<
Object
>(
Navigator
.
defaultRouteName
,
arguments:
null
));
}
else
{
}
else
{
plannedInitialRoutes
.
forEach
(
push
);
plannedInitialRoutes
.
forEach
(
push
);
}
}
}
else
{
}
else
{
Route
<
Object
>
route
;
Route
<
Object
>
route
;
if
(
initialRouteName
!=
Navigator
.
defaultRouteName
)
if
(
initialRouteName
!=
Navigator
.
defaultRouteName
)
route
=
_routeNamed
<
Object
>(
initialRouteName
,
allowNull:
true
);
route
=
_routeNamed
<
Object
>(
initialRouteName
,
allowNull:
true
,
arguments:
null
);
route
??=
_routeNamed
<
Object
>(
Navigator
.
defaultRouteName
);
route
??=
_routeNamed
<
Object
>(
Navigator
.
defaultRouteName
,
arguments:
null
);
push
(
route
);
push
(
route
);
}
}
for
(
Route
<
dynamic
>
route
in
_history
)
for
(
Route
<
dynamic
>
route
in
_history
)
...
@@ -1416,12 +1503,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1416,12 +1503,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
bool
_debugLocked
=
false
;
// used to prevent re-entrant calls to push, pop, and friends
bool
_debugLocked
=
false
;
// used to prevent re-entrant calls to push, pop, and friends
Route
<
T
>
_routeNamed
<
T
>(
String
name
,
{
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
);
final
RouteSettings
settings
=
RouteSettings
(
final
RouteSettings
settings
=
RouteSettings
(
name:
name
,
name:
name
,
isInitialRoute:
_history
.
isEmpty
,
isInitialRoute:
_history
.
isEmpty
,
arguments:
arguments
,
);
);
Route
<
T
>
route
=
widget
.
onGenerateRoute
(
settings
);
Route
<
T
>
route
=
widget
.
onGenerateRoute
(
settings
);
if
(
route
==
null
&&
!
allowNull
)
{
if
(
route
==
null
&&
!
allowNull
)
{
...
@@ -1458,6 +1546,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1458,6 +1546,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
///
///
/// {@macro flutter.widgets.navigator.pushNamed}
/// {@macro flutter.widgets.navigator.pushNamed}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
...
@@ -1469,8 +1559,11 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1469,8 +1559,11 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
Future
<
T
>
pushNamed
<
T
extends
Object
>(
String
routeName
)
{
Future
<
T
>
pushNamed
<
T
extends
Object
>(
return
push
<
T
>(
_routeNamed
<
T
>(
routeName
));
String
routeName
,
{
Object
arguments
,
})
{
return
push
<
T
>(
_routeNamed
<
T
>(
routeName
,
arguments:
arguments
));
}
}
/// Replace the current route of the navigator by pushing the route named
/// Replace the current route of the navigator by pushing the route named
...
@@ -1479,6 +1572,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1479,6 +1572,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
///
///
/// {@macro flutter.widgets.navigator.pushReplacementNamed}
/// {@macro flutter.widgets.navigator.pushReplacementNamed}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
...
@@ -1490,8 +1585,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1490,8 +1585,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
Future
<
T
>
pushReplacementNamed
<
T
extends
Object
,
TO
extends
Object
>(
String
routeName
,
{
TO
result
})
{
Future
<
T
>
pushReplacementNamed
<
T
extends
Object
,
TO
extends
Object
>(
return
pushReplacement
<
T
,
TO
>(
_routeNamed
<
T
>(
routeName
),
result:
result
);
String
routeName
,
{
TO
result
,
Object
arguments
,
})
{
return
pushReplacement
<
T
,
TO
>(
_routeNamed
<
T
>(
routeName
,
arguments:
arguments
),
result:
result
);
}
}
/// Pop the current route off the navigator and push a named route in its
/// Pop the current route off the navigator and push a named route in its
...
@@ -1499,6 +1598,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1499,6 +1598,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
///
///
/// {@macro flutter.widgets.navigator.popAndPushNamed}
/// {@macro flutter.widgets.navigator.popAndPushNamed}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
...
@@ -1510,9 +1611,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1510,9 +1611,13 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
Future
<
T
>
popAndPushNamed
<
T
extends
Object
,
TO
extends
Object
>(
String
routeName
,
{
TO
result
})
{
Future
<
T
>
popAndPushNamed
<
T
extends
Object
,
TO
extends
Object
>(
String
routeName
,
{
TO
result
,
Object
arguments
,
})
{
pop
<
TO
>(
result
);
pop
<
TO
>(
result
);
return
pushNamed
<
T
>(
routeName
);
return
pushNamed
<
T
>(
routeName
,
arguments:
arguments
);
}
}
/// 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
...
@@ -1520,6 +1625,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1520,6 +1625,8 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
///
///
/// {@macro flutter.widgets.navigator.pushNamedAndRemoveUntil}
/// {@macro flutter.widgets.navigator.pushNamedAndRemoveUntil}
///
///
/// {@macro flutter.widgets.navigator.pushNamed.arguments}
///
/// {@tool sample}
/// {@tool sample}
///
///
/// Typical usage is as follows:
/// Typical usage is as follows:
...
@@ -1531,8 +1638,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
...
@@ -1531,8 +1638,12 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
/// ```
/// ```
/// {@end-tool}
/// {@end-tool}
@optionalTypeArgs
@optionalTypeArgs
Future
<
T
>
pushNamedAndRemoveUntil
<
T
extends
Object
>(
String
newRouteName
,
RoutePredicate
predicate
)
{
Future
<
T
>
pushNamedAndRemoveUntil
<
T
extends
Object
>(
return
pushAndRemoveUntil
<
T
>(
_routeNamed
<
T
>(
newRouteName
),
predicate
);
String
newRouteName
,
RoutePredicate
predicate
,
{
Object
arguments
,
})
{
return
pushAndRemoveUntil
<
T
>(
_routeNamed
<
T
>(
newRouteName
,
arguments:
arguments
),
predicate
);
}
}
/// Push the given route onto the navigator.
/// Push the given route onto the navigator.
...
...
packages/flutter/test/widgets/navigator_test.dart
View file @
e7d63488
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
import
'dart:ui'
;
import
'dart:ui'
;
import
'package:flutter/foundation.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter/material.dart'
;
import
'package:flutter/material.dart'
;
...
@@ -870,4 +871,149 @@ void main() {
...
@@ -870,4 +871,149 @@ void main() {
semantics
.
dispose
();
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
();
});
}
}
packages/flutter/test/widgets/routes_test.dart
View file @
e7d63488
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
import
'dart:collection'
;
import
'dart:collection'
;
import
'package:flutter/foundation.dart'
;
import
'package:mockito/mockito.dart'
;
import
'package:mockito/mockito.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter_test/flutter_test.dart'
;
import
'package:flutter/widgets.dart'
;
import
'package:flutter/widgets.dart'
;
...
@@ -114,6 +115,23 @@ void main() {
...
@@ -114,6 +115,23 @@ void main() {
expect
(
settings3
.
isInitialRoute
,
true
);
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
{
testWidgets
(
'Route management - push, replace, pop'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
<
NavigatorState
>
navigatorKey
=
GlobalKey
<
NavigatorState
>();
final
GlobalKey
<
NavigatorState
>
navigatorKey
=
GlobalKey
<
NavigatorState
>();
await
tester
.
pumpWidget
(
await
tester
.
pumpWidget
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment