Add a flag to toggle navigator route update reporting (#60621)
This commit is contained in:
@@ -1308,6 +1308,7 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
|
||||
},
|
||||
onUnknownRoute: _onUnknownRoute,
|
||||
observers: widget.navigatorObservers,
|
||||
reportsRouteUpdateToEngine: true,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1397,11 +1397,13 @@ class Navigator extends StatefulWidget {
|
||||
this.onGenerateRoute,
|
||||
this.onUnknownRoute,
|
||||
this.transitionDelegate = const DefaultTransitionDelegate<dynamic>(),
|
||||
this.reportsRouteUpdateToEngine = false,
|
||||
this.observers = const <NavigatorObserver>[],
|
||||
}) : assert(pages != null),
|
||||
assert(onGenerateInitialRoutes != null),
|
||||
assert(transitionDelegate != null),
|
||||
assert(observers != null),
|
||||
assert(reportsRouteUpdateToEngine != null),
|
||||
super(key: key);
|
||||
|
||||
/// The list of pages with which to populate the history.
|
||||
@@ -1497,6 +1499,22 @@ class Navigator extends StatefulWidget {
|
||||
/// will be primed.
|
||||
final RouteListFactory onGenerateInitialRoutes;
|
||||
|
||||
/// Whether this navigator should report route update message back to the
|
||||
/// engine when the top-most route changes.
|
||||
///
|
||||
/// If the property is set to true, this navigator automatically sends the
|
||||
/// route update message to the engine when it detects top-most route changes.
|
||||
/// The messages are used by the web engine to update the browser URL bar.
|
||||
///
|
||||
/// If there are multiple navigators in the widget tree, at most one of them
|
||||
/// can set this property to true (typically, the top-most one created from
|
||||
/// the [WidgetsApp]). Otherwise, the web engine may receive multiple route
|
||||
/// update messages from different navigators and fail to update the URL
|
||||
/// bar.
|
||||
///
|
||||
/// Defaults to false.
|
||||
final bool reportsRouteUpdateToEngine;
|
||||
|
||||
/// Push a named route onto the navigator that most tightly encloses the given
|
||||
/// context.
|
||||
///
|
||||
@@ -3244,11 +3262,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
|
||||
_flushRouteAnnouncement();
|
||||
|
||||
// Announces route name changes.
|
||||
final _RouteEntry lastEntry = _history.lastWhere(_RouteEntry.isPresentPredicate, orElse: () => null);
|
||||
final String routeName = lastEntry?.route?.settings?.name;
|
||||
if (routeName != _lastAnnouncedRouteName) {
|
||||
RouteNotificationMessages.maybeNotifyRouteChange(routeName, _lastAnnouncedRouteName);
|
||||
_lastAnnouncedRouteName = routeName;
|
||||
if (widget.reportsRouteUpdateToEngine) {
|
||||
final _RouteEntry lastEntry = _history.lastWhere(
|
||||
_RouteEntry.isPresentPredicate, orElse: () => null);
|
||||
final String routeName = lastEntry?.route?.settings?.name;
|
||||
if (routeName != _lastAnnouncedRouteName) {
|
||||
RouteNotificationMessages.maybeNotifyRouteChange(
|
||||
routeName, _lastAnnouncedRouteName);
|
||||
_lastAnnouncedRouteName = routeName;
|
||||
}
|
||||
}
|
||||
|
||||
// Lastly, removes the overlay entries of all marked entries and disposes
|
||||
|
||||
@@ -103,6 +103,48 @@ void main() {
|
||||
));
|
||||
});
|
||||
|
||||
testWidgets('Navigator does not report route name by default', (WidgetTester tester) async {
|
||||
final List<MethodCall> log = <MethodCall>[];
|
||||
SystemChannels.navigation.setMockMethodCallHandler((MethodCall methodCall) async {
|
||||
log.add(methodCall);
|
||||
});
|
||||
|
||||
await tester.pumpWidget(Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Navigator(
|
||||
pages: <Page<void>>[
|
||||
TransitionBuilderPage<void>(
|
||||
name: '/',
|
||||
pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) => const Placeholder(),
|
||||
),
|
||||
],
|
||||
onPopPage: (Route<void> route, void result) => false,
|
||||
)
|
||||
));
|
||||
|
||||
expect(log, hasLength(0));
|
||||
|
||||
await tester.pumpWidget(Directionality(
|
||||
textDirection: TextDirection.ltr,
|
||||
child: Navigator(
|
||||
pages: <Page<void>>[
|
||||
TransitionBuilderPage<void>(
|
||||
name: '/',
|
||||
pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) => const Placeholder(),
|
||||
),
|
||||
TransitionBuilderPage<void>(
|
||||
name: '/abc',
|
||||
pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) => const Placeholder(),
|
||||
),
|
||||
],
|
||||
onPopPage: (Route<void> route, void result) => false,
|
||||
)
|
||||
));
|
||||
|
||||
await tester.pumpAndSettle();
|
||||
expect(log, hasLength(0));
|
||||
});
|
||||
|
||||
testWidgets('Replace should send platform messages', (WidgetTester tester) async {
|
||||
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
|
||||
'/': (BuildContext context) => OnTapPage(
|
||||
|
||||
Reference in New Issue
Block a user