Unverified Commit 00b1903b authored by chunhtai's avatar chunhtai Committed by GitHub

Add a flag to toggle navigator route update reporting (#60621)

parent 5a00d54e
...@@ -1308,6 +1308,7 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver { ...@@ -1308,6 +1308,7 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
}, },
onUnknownRoute: _onUnknownRoute, onUnknownRoute: _onUnknownRoute,
observers: widget.navigatorObservers, observers: widget.navigatorObservers,
reportsRouteUpdateToEngine: true,
); );
} }
......
...@@ -1397,11 +1397,13 @@ class Navigator extends StatefulWidget { ...@@ -1397,11 +1397,13 @@ class Navigator extends StatefulWidget {
this.onGenerateRoute, this.onGenerateRoute,
this.onUnknownRoute, this.onUnknownRoute,
this.transitionDelegate = const DefaultTransitionDelegate<dynamic>(), this.transitionDelegate = const DefaultTransitionDelegate<dynamic>(),
this.reportsRouteUpdateToEngine = false,
this.observers = const <NavigatorObserver>[], this.observers = const <NavigatorObserver>[],
}) : assert(pages != null), }) : assert(pages != null),
assert(onGenerateInitialRoutes != null), assert(onGenerateInitialRoutes != null),
assert(transitionDelegate != null), assert(transitionDelegate != null),
assert(observers != null), assert(observers != null),
assert(reportsRouteUpdateToEngine != null),
super(key: key); super(key: key);
/// The list of pages with which to populate the history. /// The list of pages with which to populate the history.
...@@ -1497,6 +1499,22 @@ class Navigator extends StatefulWidget { ...@@ -1497,6 +1499,22 @@ class Navigator extends StatefulWidget {
/// will be primed. /// will be primed.
final RouteListFactory onGenerateInitialRoutes; 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 /// Push a named route onto the navigator that most tightly encloses the given
/// context. /// context.
/// ///
...@@ -3244,11 +3262,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin { ...@@ -3244,11 +3262,15 @@ class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
_flushRouteAnnouncement(); _flushRouteAnnouncement();
// Announces route name changes. // Announces route name changes.
final _RouteEntry lastEntry = _history.lastWhere(_RouteEntry.isPresentPredicate, orElse: () => null); if (widget.reportsRouteUpdateToEngine) {
final String routeName = lastEntry?.route?.settings?.name; final _RouteEntry lastEntry = _history.lastWhere(
if (routeName != _lastAnnouncedRouteName) { _RouteEntry.isPresentPredicate, orElse: () => null);
RouteNotificationMessages.maybeNotifyRouteChange(routeName, _lastAnnouncedRouteName); final String routeName = lastEntry?.route?.settings?.name;
_lastAnnouncedRouteName = routeName; if (routeName != _lastAnnouncedRouteName) {
RouteNotificationMessages.maybeNotifyRouteChange(
routeName, _lastAnnouncedRouteName);
_lastAnnouncedRouteName = routeName;
}
} }
// Lastly, removes the overlay entries of all marked entries and disposes // Lastly, removes the overlay entries of all marked entries and disposes
......
...@@ -103,6 +103,48 @@ void main() { ...@@ -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 { testWidgets('Replace should send platform messages', (WidgetTester tester) async {
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{ final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
'/': (BuildContext context) => OnTapPage( '/': (BuildContext context) => OnTapPage(
......
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