From 00b1903bbc4911021c4839a39901bdaf95b1f4ac Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Wed, 8 Jul 2020 18:16:04 -0700 Subject: [PATCH] Add a flag to toggle navigator route update reporting (#60621) --- packages/flutter/lib/src/widgets/app.dart | 1 + .../flutter/lib/src/widgets/navigator.dart | 32 +++++++++++--- .../route_notification_messages_test.dart | 42 +++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 33e9a6146f..0466b8f6f5 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -1308,6 +1308,7 @@ class _WidgetsAppState extends State with WidgetsBindingObserver { }, onUnknownRoute: _onUnknownRoute, observers: widget.navigatorObservers, + reportsRouteUpdateToEngine: true, ); } diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 5da966bce7..1e0ddb74e9 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -1397,11 +1397,13 @@ class Navigator extends StatefulWidget { this.onGenerateRoute, this.onUnknownRoute, this.transitionDelegate = const DefaultTransitionDelegate(), + this.reportsRouteUpdateToEngine = false, this.observers = const [], }) : 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 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 diff --git a/packages/flutter/test/widgets/route_notification_messages_test.dart b/packages/flutter/test/widgets/route_notification_messages_test.dart index 3752f7040a..9c967d2afd 100644 --- a/packages/flutter/test/widgets/route_notification_messages_test.dart +++ b/packages/flutter/test/widgets/route_notification_messages_test.dart @@ -103,6 +103,48 @@ void main() { )); }); + testWidgets('Navigator does not report route name by default', (WidgetTester tester) async { + final List log = []; + SystemChannels.navigation.setMockMethodCallHandler((MethodCall methodCall) async { + log.add(methodCall); + }); + + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Navigator( + pages: >[ + TransitionBuilderPage( + name: '/', + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), + ), + ], + onPopPage: (Route route, void result) => false, + ) + )); + + expect(log, hasLength(0)); + + await tester.pumpWidget(Directionality( + textDirection: TextDirection.ltr, + child: Navigator( + pages: >[ + TransitionBuilderPage( + name: '/', + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), + ), + TransitionBuilderPage( + name: '/abc', + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), + ), + ], + onPopPage: (Route route, void result) => false, + ) + )); + + await tester.pumpAndSettle(); + expect(log, hasLength(0)); + }); + testWidgets('Replace should send platform messages', (WidgetTester tester) async { final Map routes = { '/': (BuildContext context) => OnTapPage(