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
17ed4e0b
Unverified
Commit
17ed4e0b
authored
Jun 08, 2020
by
chunhtai
Committed by
GitHub
Jun 08, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce inherited navigator observer and refactor hero controller (#58808)
parent
333eb9d7
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
288 additions
and
149 deletions
+288
-149
app.dart
packages/flutter/lib/src/cupertino/app.dart
+44
-56
app.dart
packages/flutter/lib/src/material/app.dart
+62
-75
navigator.dart
packages/flutter/lib/src/widgets/navigator.dart
+99
-18
debug_test.dart
packages/flutter/test/material/debug_test.dart
+2
-0
navigator_test.dart
packages/flutter/test/widgets/navigator_test.dart
+81
-0
No files found.
packages/flutter/lib/src/cupertino/app.dart
View file @
17ed4e0b
...
...
@@ -289,7 +289,6 @@ class _CupertinoAppState extends State<CupertinoApp> {
void
initState
()
{
super
.
initState
();
_heroController
=
CupertinoApp
.
createCupertinoHeroController
();
_updateNavigator
();
}
@override
...
...
@@ -302,21 +301,6 @@ class _CupertinoAppState extends State<CupertinoApp> {
// Navigator has a GlobalKey).
_heroController
=
CupertinoApp
.
createCupertinoHeroController
();
}
_updateNavigator
();
}
List
<
NavigatorObserver
>
_navigatorObservers
;
void
_updateNavigator
()
{
if
(
widget
.
home
!=
null
||
widget
.
routes
.
isNotEmpty
||
widget
.
onGenerateRoute
!=
null
||
widget
.
onUnknownRoute
!=
null
)
{
_navigatorObservers
=
List
<
NavigatorObserver
>.
from
(
widget
.
navigatorObservers
)
..
add
(
_heroController
);
}
else
{
_navigatorObservers
=
const
<
NavigatorObserver
>[];
}
}
// Combine the default localization for Cupertino with the ones contributed
...
...
@@ -342,46 +326,50 @@ class _CupertinoAppState extends State<CupertinoApp> {
data:
effectiveThemeData
,
child:
Builder
(
builder:
(
BuildContext
context
)
{
return
WidgetsApp
(
key:
GlobalObjectKey
(
this
),
navigatorKey:
widget
.
navigatorKey
,
navigatorObservers:
_navigatorObservers
,
pageRouteBuilder:
<
T
>(
RouteSettings
settings
,
WidgetBuilder
builder
)
=>
CupertinoPageRoute
<
T
>(
settings:
settings
,
builder:
builder
),
home:
widget
.
home
,
routes:
widget
.
routes
,
initialRoute:
widget
.
initialRoute
,
onGenerateRoute:
widget
.
onGenerateRoute
,
onGenerateInitialRoutes:
widget
.
onGenerateInitialRoutes
,
onUnknownRoute:
widget
.
onUnknownRoute
,
builder:
widget
.
builder
,
title:
widget
.
title
,
onGenerateTitle:
widget
.
onGenerateTitle
,
textStyle:
CupertinoTheme
.
of
(
context
).
textTheme
.
textStyle
,
color:
CupertinoDynamicColor
.
resolve
(
widget
.
color
??
effectiveThemeData
.
primaryColor
,
context
),
locale:
widget
.
locale
,
localizationsDelegates:
_localizationsDelegates
,
localeResolutionCallback:
widget
.
localeResolutionCallback
,
localeListResolutionCallback:
widget
.
localeListResolutionCallback
,
supportedLocales:
widget
.
supportedLocales
,
showPerformanceOverlay:
widget
.
showPerformanceOverlay
,
checkerboardRasterCacheImages:
widget
.
checkerboardRasterCacheImages
,
checkerboardOffscreenLayers:
widget
.
checkerboardOffscreenLayers
,
showSemanticsDebugger:
widget
.
showSemanticsDebugger
,
debugShowCheckedModeBanner:
widget
.
debugShowCheckedModeBanner
,
inspectorSelectButtonBuilder:
(
BuildContext
context
,
VoidCallback
onPressed
)
{
return
CupertinoButton
.
filled
(
child:
const
Icon
(
CupertinoIcons
.
search
,
size:
28.0
,
color:
CupertinoColors
.
white
,
),
padding:
EdgeInsets
.
zero
,
onPressed:
onPressed
,
);
},
shortcuts:
widget
.
shortcuts
,
actions:
widget
.
actions
,
return
HeroControllerScope
(
controller:
_heroController
,
child:
WidgetsApp
(
key:
GlobalObjectKey
(
this
),
navigatorKey:
widget
.
navigatorKey
,
navigatorObservers:
widget
.
navigatorObservers
,
pageRouteBuilder:
<
T
>(
RouteSettings
settings
,
WidgetBuilder
builder
)
=>
CupertinoPageRoute
<
T
>(
settings:
settings
,
builder:
builder
),
home:
widget
.
home
,
routes:
widget
.
routes
,
initialRoute:
widget
.
initialRoute
,
onGenerateRoute:
widget
.
onGenerateRoute
,
onGenerateInitialRoutes:
widget
.
onGenerateInitialRoutes
,
onUnknownRoute:
widget
.
onUnknownRoute
,
builder:
widget
.
builder
,
title:
widget
.
title
,
onGenerateTitle:
widget
.
onGenerateTitle
,
textStyle:
CupertinoTheme
.
of
(
context
).
textTheme
.
textStyle
,
color:
CupertinoDynamicColor
.
resolve
(
widget
.
color
??
effectiveThemeData
.
primaryColor
,
context
),
locale:
widget
.
locale
,
localizationsDelegates:
_localizationsDelegates
,
localeResolutionCallback:
widget
.
localeResolutionCallback
,
localeListResolutionCallback:
widget
.
localeListResolutionCallback
,
supportedLocales:
widget
.
supportedLocales
,
showPerformanceOverlay:
widget
.
showPerformanceOverlay
,
checkerboardRasterCacheImages:
widget
.
checkerboardRasterCacheImages
,
checkerboardOffscreenLayers:
widget
.
checkerboardOffscreenLayers
,
showSemanticsDebugger:
widget
.
showSemanticsDebugger
,
debugShowCheckedModeBanner:
widget
.
debugShowCheckedModeBanner
,
inspectorSelectButtonBuilder:
(
BuildContext
context
,
VoidCallback
onPressed
)
{
return
CupertinoButton
.
filled
(
child:
const
Icon
(
CupertinoIcons
.
search
,
size:
28.0
,
color:
CupertinoColors
.
white
,
),
padding:
EdgeInsets
.
zero
,
onPressed:
onPressed
,
);
},
shortcuts:
widget
.
shortcuts
,
actions:
widget
.
actions
,
),
);
},
),
...
...
packages/flutter/lib/src/material/app.dart
View file @
17ed4e0b
...
...
@@ -571,7 +571,6 @@ class _MaterialAppState extends State<MaterialApp> {
void
initState
()
{
super
.
initState
();
_heroController
=
HeroController
(
createRectTween:
_createRectTween
);
_updateNavigator
();
}
@override
...
...
@@ -584,21 +583,6 @@ class _MaterialAppState extends State<MaterialApp> {
// Navigator has a GlobalKey).
_heroController
=
HeroController
(
createRectTween:
_createRectTween
);
}
_updateNavigator
();
}
List
<
NavigatorObserver
>
_navigatorObservers
;
void
_updateNavigator
()
{
if
(
widget
.
home
!=
null
||
widget
.
routes
.
isNotEmpty
||
widget
.
onGenerateRoute
!=
null
||
widget
.
onUnknownRoute
!=
null
)
{
_navigatorObservers
=
List
<
NavigatorObserver
>.
from
(
widget
.
navigatorObservers
)
..
add
(
_heroController
);
}
else
{
_navigatorObservers
=
const
<
NavigatorObserver
>[];
}
}
RectTween
_createRectTween
(
Rect
begin
,
Rect
end
)
{
...
...
@@ -619,36 +603,38 @@ class _MaterialAppState extends State<MaterialApp> {
@override
Widget
build
(
BuildContext
context
)
{
Widget
result
=
WidgetsApp
(
key:
GlobalObjectKey
(
this
),
navigatorKey:
widget
.
navigatorKey
,
navigatorObservers:
_navigatorObservers
,
pageRouteBuilder:
<
T
>(
RouteSettings
settings
,
WidgetBuilder
builder
)
{
return
MaterialPageRoute
<
T
>(
settings:
settings
,
builder:
builder
);
},
home:
widget
.
home
,
routes:
widget
.
routes
,
initialRoute:
widget
.
initialRoute
,
onGenerateRoute:
widget
.
onGenerateRoute
,
onGenerateInitialRoutes:
widget
.
onGenerateInitialRoutes
,
onUnknownRoute:
widget
.
onUnknownRoute
,
builder:
(
BuildContext
context
,
Widget
child
)
{
// Use a light theme, dark theme, or fallback theme.
final
ThemeMode
mode
=
widget
.
themeMode
??
ThemeMode
.
system
;
ThemeData
theme
;
if
(
widget
.
darkTheme
!=
null
)
{
final
ui
.
Brightness
platformBrightness
=
MediaQuery
.
platformBrightnessOf
(
context
);
if
(
mode
==
ThemeMode
.
dark
||
Widget
result
=
HeroControllerScope
(
controller:
_heroController
,
child:
WidgetsApp
(
key:
GlobalObjectKey
(
this
),
navigatorKey:
widget
.
navigatorKey
,
navigatorObservers:
widget
.
navigatorObservers
,
pageRouteBuilder:
<
T
>(
RouteSettings
settings
,
WidgetBuilder
builder
)
{
return
MaterialPageRoute
<
T
>(
settings:
settings
,
builder:
builder
);
},
home:
widget
.
home
,
routes:
widget
.
routes
,
initialRoute:
widget
.
initialRoute
,
onGenerateRoute:
widget
.
onGenerateRoute
,
onGenerateInitialRoutes:
widget
.
onGenerateInitialRoutes
,
onUnknownRoute:
widget
.
onUnknownRoute
,
builder:
(
BuildContext
context
,
Widget
child
)
{
// Use a light theme, dark theme, or fallback theme.
final
ThemeMode
mode
=
widget
.
themeMode
??
ThemeMode
.
system
;
ThemeData
theme
;
if
(
widget
.
darkTheme
!=
null
)
{
final
ui
.
Brightness
platformBrightness
=
MediaQuery
.
platformBrightnessOf
(
context
);
if
(
mode
==
ThemeMode
.
dark
||
(
mode
==
ThemeMode
.
system
&&
platformBrightness
==
ui
.
Brightness
.
dark
))
{
theme
=
widget
.
darkTheme
;
theme
=
widget
.
darkTheme
;
}
}
}
theme
??=
widget
.
theme
??
ThemeData
.
fallback
();
theme
??=
widget
.
theme
??
ThemeData
.
fallback
();
return
AnimatedTheme
(
data:
theme
,
isMaterialAppTheme:
true
,
child:
widget
.
builder
!=
null
return
AnimatedTheme
(
data:
theme
,
isMaterialAppTheme:
true
,
child:
widget
.
builder
!=
null
?
Builder
(
builder:
(
BuildContext
context
)
{
// Why are we surrounding a builder with a builder?
...
...
@@ -666,38 +652,39 @@ class _MaterialAppState extends State<MaterialApp> {
},
)
:
child
,
);
},
title:
widget
.
title
,
onGenerateTitle:
widget
.
onGenerateTitle
,
textStyle:
_errorTextStyle
,
// The color property is always pulled from the light theme, even if dark
// mode is activated. This was done to simplify the technical details
// of switching themes and it was deemed acceptable because this color
// property is only used on old Android OSes to color the app bar in
// Android's switcher UI.
//
// blue is the primary color of the default theme
color:
widget
.
color
??
widget
.
theme
?.
primaryColor
??
Colors
.
blue
,
locale:
widget
.
locale
,
localizationsDelegates:
_localizationsDelegates
,
localeResolutionCallback:
widget
.
localeResolutionCallback
,
localeListResolutionCallback:
widget
.
localeListResolutionCallback
,
supportedLocales:
widget
.
supportedLocales
,
showPerformanceOverlay:
widget
.
showPerformanceOverlay
,
checkerboardRasterCacheImages:
widget
.
checkerboardRasterCacheImages
,
checkerboardOffscreenLayers:
widget
.
checkerboardOffscreenLayers
,
showSemanticsDebugger:
widget
.
showSemanticsDebugger
,
debugShowCheckedModeBanner:
widget
.
debugShowCheckedModeBanner
,
inspectorSelectButtonBuilder:
(
BuildContext
context
,
VoidCallback
onPressed
)
{
return
FloatingActionButton
(
child:
const
Icon
(
Icons
.
search
),
onPressed:
onPressed
,
mini:
true
,
);
},
shortcuts:
widget
.
shortcuts
,
actions:
widget
.
actions
,
);
},
title:
widget
.
title
,
onGenerateTitle:
widget
.
onGenerateTitle
,
textStyle:
_errorTextStyle
,
// The color property is always pulled from the light theme, even if dark
// mode is activated. This was done to simplify the technical details
// of switching themes and it was deemed acceptable because this color
// property is only used on old Android OSes to color the app bar in
// Android's switcher UI.
//
// blue is the primary color of the default theme
color:
widget
.
color
??
widget
.
theme
?.
primaryColor
??
Colors
.
blue
,
locale:
widget
.
locale
,
localizationsDelegates:
_localizationsDelegates
,
localeResolutionCallback:
widget
.
localeResolutionCallback
,
localeListResolutionCallback:
widget
.
localeListResolutionCallback
,
supportedLocales:
widget
.
supportedLocales
,
showPerformanceOverlay:
widget
.
showPerformanceOverlay
,
checkerboardRasterCacheImages:
widget
.
checkerboardRasterCacheImages
,
checkerboardOffscreenLayers:
widget
.
checkerboardOffscreenLayers
,
showSemanticsDebugger:
widget
.
showSemanticsDebugger
,
debugShowCheckedModeBanner:
widget
.
debugShowCheckedModeBanner
,
inspectorSelectButtonBuilder:
(
BuildContext
context
,
VoidCallback
onPressed
)
{
return
FloatingActionButton
(
child:
const
Icon
(
Icons
.
search
),
onPressed:
onPressed
,
mini:
true
,
);
},
shortcuts:
widget
.
shortcuts
,
actions:
widget
.
actions
,
),
);
assert
(()
{
...
...
packages/flutter/lib/src/widgets/navigator.dart
View file @
17ed4e0b
...
...
@@ -17,6 +17,7 @@ import 'binding.dart';
import
'focus_manager.dart'
;
import
'focus_scope.dart'
;
import
'framework.dart'
;
import
'heroes.dart'
;
import
'overlay.dart'
;
import
'route_notification_messages.dart'
;
import
'routes.dart'
;
...
...
@@ -608,6 +609,45 @@ class NavigatorObserver {
void
didStopUserGesture
()
{
}
}
/// An inherited widget to host a hero controller.
///
/// This class should not be used directly. The [MaterialApp] and [CupertinoApp]
/// use this class to host the [HeroController], and they should be the only
/// exception to use this class. If you want to subscribe your own
/// [HeroController], use the [Navigator.observers] instead.
///
/// The hosted hero controller will be picked up by the navigator in the
/// [child] subtree. Once a navigator picks up this controller, the navigator
/// will bar any navigator below its subtree from receiving this controller.
///
/// See also:
///
/// * [Navigator.observers], which is the standard way of providing a
/// [HeroController].
class
HeroControllerScope
extends
InheritedWidget
{
/// Creates a widget to host the input [controller].
const
HeroControllerScope
({
Key
key
,
this
.
controller
,
Widget
child
,
})
:
super
(
key:
key
,
child:
child
);
/// The hero controller that is hosted inside this widget.
final
HeroController
controller
;
/// Retrieves the [HeroController] from the closest [HeroControllerScope]
/// ancestor.
static
HeroController
of
(
BuildContext
context
)
{
final
HeroControllerScope
host
=
context
.
dependOnInheritedWidgetOfExactType
<
HeroControllerScope
>();
return
host
?.
controller
;
}
@override
bool
updateShouldNotify
(
HeroControllerScope
oldWidget
)
{
return
oldWidget
.
controller
!=
controller
;
}
}
/// A [Route] wrapper interface that can be staged for [TransitionDelegate] to
/// decide how its underlying [Route] should transition on or off screen.
abstract
class
RouteTransitionRecord
{
...
...
@@ -2664,6 +2704,10 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
bool
_debugLocked
=
false
;
// used to prevent re-entrant calls to push, pop, and friends
HeroController
_heroControllerFromScope
;
List
<
NavigatorObserver
>
_effectiveObservers
;
@override
void
initState
()
{
super
.
initState
();
...
...
@@ -2675,6 +2719,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
assert
(
observer
.
navigator
==
null
);
observer
.
_navigator
=
this
;
}
_effectiveObservers
=
widget
.
observers
;
// We have to manually extract the inherited widget in initState because
// the current context is not fully initialized.
final
HeroControllerScope
heroControllerScope
=
context
.
getElementForInheritedWidgetOfExactType
<
HeroControllerScope
>()
?.
widget
as
HeroControllerScope
;
_updateHeroController
(
heroControllerScope
?.
controller
);
String
initialRoute
=
widget
.
initialRoute
;
if
(
widget
.
pages
.
isNotEmpty
)
{
_history
.
addAll
(
...
...
@@ -2707,6 +2760,28 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
assert
(()
{
_debugLocked
=
false
;
return
true
;
}());
}
@override
void
didChangeDependencies
()
{
super
.
didChangeDependencies
();
_updateHeroController
(
HeroControllerScope
.
of
(
context
));
}
void
_updateHeroController
(
HeroController
newHeroController
)
{
if
(
_heroControllerFromScope
!=
newHeroController
)
{
_heroControllerFromScope
?.
_navigator
=
null
;
newHeroController
?.
_navigator
=
this
;
_heroControllerFromScope
=
newHeroController
;
_updateEffectiveObservers
();
}
}
void
_updateEffectiveObservers
()
{
if
(
_heroControllerFromScope
!=
null
)
_effectiveObservers
=
widget
.
observers
+
<
NavigatorObserver
>[
_heroControllerFromScope
];
else
_effectiveObservers
=
widget
.
observers
;
}
@override
void
didUpdateWidget
(
Navigator
oldWidget
)
{
super
.
didUpdateWidget
(
oldWidget
);
...
...
@@ -2721,6 +2796,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
assert
(
observer
.
navigator
==
null
);
observer
.
_navigator
=
this
;
}
_updateEffectiveObservers
();
}
if
(
oldWidget
.
pages
!=
widget
.
pages
)
{
assert
(
...
...
@@ -2754,7 +2830,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
_debugLocked
=
true
;
return
true
;
}());
for
(
final
NavigatorObserver
observer
in
widget
.
o
bservers
)
for
(
final
NavigatorObserver
observer
in
_effectiveO
bservers
)
observer
.
_navigator
=
null
;
focusScopeNode
.
dispose
();
for
(
final
_RouteEntry
entry
in
_history
)
...
...
@@ -3181,19 +3257,19 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
}
void
_flushObserverNotifications
()
{
if
(
widget
.
o
bservers
.
isEmpty
)
{
if
(
_effectiveO
bservers
.
isEmpty
)
{
_observedRouteDeletions
.
clear
();
_observedRouteAdditions
.
clear
();
return
;
}
while
(
_observedRouteAdditions
.
isNotEmpty
)
{
final
_NavigatorObservation
observation
=
_observedRouteAdditions
.
removeLast
();
widget
.
o
bservers
.
forEach
(
observation
.
notify
);
_effectiveO
bservers
.
forEach
(
observation
.
notify
);
}
while
(
_observedRouteDeletions
.
isNotEmpty
)
{
final
_NavigatorObservation
observation
=
_observedRouteDeletions
.
removeFirst
();
widget
.
o
bservers
.
forEach
(
observation
.
notify
);
_effectiveO
bservers
.
forEach
(
observation
.
notify
);
}
}
...
...
@@ -3885,7 +3961,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
_RouteEntry
.
willBePresentPredicate
,
).
route
;
}
for
(
final
NavigatorObserver
observer
in
widget
.
o
bservers
)
for
(
final
NavigatorObserver
observer
in
_effectiveO
bservers
)
observer
.
didStartUserGesture
(
route
,
previousRoute
);
}
}
...
...
@@ -3898,7 +3974,7 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
assert
(
_userGesturesInProgress
>
0
);
_userGesturesInProgress
-=
1
;
if
(
_userGesturesInProgress
==
0
)
{
for
(
final
NavigatorObserver
observer
in
widget
.
o
bservers
)
for
(
final
NavigatorObserver
observer
in
_effectiveO
bservers
)
observer
.
didStopUserGesture
();
}
}
...
...
@@ -3933,18 +4009,23 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
Widget
build
(
BuildContext
context
)
{
assert
(!
_debugLocked
);
assert
(
_history
.
isNotEmpty
);
return
Listener
(
onPointerDown:
_handlePointerDown
,
onPointerUp:
_handlePointerUpOrCancel
,
onPointerCancel:
_handlePointerUpOrCancel
,
child:
AbsorbPointer
(
absorbing:
false
,
// it's mutated directly by _cancelActivePointers above
child:
FocusScope
(
node:
focusScopeNode
,
autofocus:
true
,
child:
Overlay
(
key:
_overlayKey
,
initialEntries:
overlay
==
null
?
_allRouteOverlayEntries
.
toList
(
growable:
false
)
:
const
<
OverlayEntry
>[],
// Hides the HeroControllerScope for the widget subtree so that the other
// nested navigator underneath will not pick up the hero controller above
// this level.
return
HeroControllerScope
(
child:
Listener
(
onPointerDown:
_handlePointerDown
,
onPointerUp:
_handlePointerUpOrCancel
,
onPointerCancel:
_handlePointerUpOrCancel
,
child:
AbsorbPointer
(
absorbing:
false
,
// it's mutated directly by _cancelActivePointers above
child:
FocusScope
(
node:
focusScopeNode
,
autofocus:
true
,
child:
Overlay
(
key:
_overlayKey
,
initialEntries:
overlay
==
null
?
_allRouteOverlayEntries
.
toList
(
growable:
false
)
:
const
<
OverlayEntry
>[],
),
),
),
),
...
...
packages/flutter/test/material/debug_test.dart
View file @
17ed4e0b
...
...
@@ -150,6 +150,7 @@ void main() {
' AbsorbPointer
\n
'
' _PointerListener
\n
'
' Listener
\n
'
' HeroControllerScope
\n
'
' Navigator-[GlobalObjectKey<NavigatorState> _WidgetsAppState#00000]
\n
'
' IconTheme
\n
'
' IconTheme
\n
'
...
...
@@ -182,6 +183,7 @@ void main() {
' Focus
\n
'
' Shortcuts
\n
'
' WidgetsApp-[GlobalObjectKey _MaterialAppState#00000]
\n
'
' HeroControllerScope
\n
'
' ScrollConfiguration
\n
'
' MaterialApp
\n
'
' [root]
\n
'
...
...
packages/flutter/test/widgets/navigator_test.dart
View file @
17ed4e0b
...
...
@@ -1965,6 +1965,77 @@ void main() {
expect
(
popNextOfFirst
,
secondRoute
);
});
testWidgets
(
'hero controller scope works'
,
(
WidgetTester
tester
)
async
{
final
GlobalKey
<
NavigatorState
>
top
=
GlobalKey
<
NavigatorState
>();
final
GlobalKey
<
NavigatorState
>
sub
=
GlobalKey
<
NavigatorState
>();
final
List
<
NavigatorObservation
>
observations
=
<
NavigatorObservation
>[];
final
HeroControllerSpy
spy
=
HeroControllerSpy
()
..
onPushed
=
(
Route
<
dynamic
>
route
,
Route
<
dynamic
>
previousRoute
)
{
observations
.
add
(
NavigatorObservation
(
current:
route
?.
settings
?.
name
,
previous:
previousRoute
?.
settings
?.
name
,
operation:
'didPush'
)
);
};
await
tester
.
pumpWidget
(
HeroControllerScope
(
controller:
spy
,
child:
Directionality
(
textDirection:
TextDirection
.
ltr
,
child:
Navigator
(
key:
top
,
initialRoute:
'top1'
,
onGenerateRoute:
(
RouteSettings
s
)
{
return
MaterialPageRoute
<
void
>(
builder:
(
BuildContext
c
)
{
return
Navigator
(
key:
sub
,
initialRoute:
'sub1'
,
onGenerateRoute:
(
RouteSettings
s
)
{
return
MaterialPageRoute
<
void
>(
builder:
(
BuildContext
c
)
{
return
const
Placeholder
();
},
settings:
s
,
);
},
);
},
settings:
s
,
);
},
),
)
)
);
// It should only observe the top navigator.
expect
(
observations
.
length
,
1
);
expect
(
observations
[
0
].
current
,
'top1'
);
expect
(
observations
[
0
].
previous
,
isNull
);
sub
.
currentState
.
push
(
MaterialPageRoute
<
void
>(
settings:
const
RouteSettings
(
name:
'sub2'
),
builder:
(
BuildContext
context
)
=>
const
Text
(
'sub2'
)
));
await
tester
.
pumpAndSettle
();
expect
(
find
.
text
(
'sub2'
),
findsOneWidget
);
// It should not record sub navigator.
expect
(
observations
.
length
,
1
);
top
.
currentState
.
push
(
MaterialPageRoute
<
void
>(
settings:
const
RouteSettings
(
name:
'top2'
),
builder:
(
BuildContext
context
)
=>
const
Text
(
'top2'
)
));
await
tester
.
pumpAndSettle
();
expect
(
observations
.
length
,
2
);
expect
(
observations
[
1
].
current
,
'top2'
);
expect
(
observations
[
1
].
previous
,
'top1'
);
});
group
(
'Page api'
,
(){
Widget
buildNavigator
({
List
<
Page
<
dynamic
>>
pages
,
...
...
@@ -3024,6 +3095,16 @@ class StatefulTestState extends State<StatefulTestWidget> {
}
}
class
HeroControllerSpy
extends
HeroController
{
OnObservation
onPushed
;
@override
void
didPush
(
Route
<
dynamic
>
route
,
Route
<
dynamic
>
previousRoute
)
{
if
(
onPushed
!=
null
)
{
onPushed
(
route
,
previousRoute
);
}
}
}
class
NavigatorObservation
{
const
NavigatorObservation
({
this
.
previous
,
this
.
current
,
this
.
operation
});
final
String
previous
;
...
...
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