diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index eb86bbbec0..7500ca98c6 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -77,18 +77,6 @@ enum RoutePopDisposition { bubble, } -/// Name for the method which is used for sending messages from framework to -/// engine after a route is popped. -const String _routePoppedMethod = 'routePopped'; - -/// Name for the method which is used for sending messages from framework to -/// engine after a route is pushed. -const String _routePushedMethod = 'routePushed'; - -/// Name for the method which is used for sending messages from framework to -/// engine after a route is replaced. -const String _routeReplacedMethod = 'routeReplaced'; - /// An abstraction for an entry managed by a [Navigator]. /// /// This class defines an abstract interface between the navigator and the @@ -1793,7 +1781,6 @@ class _RouteEntry { if (isNewFirst) { route.didChangeNext(null); } - RouteNotificationMessages.maybeNotifyRouteChange(_routePushedMethod, route, previous); for (final NavigatorObserver observer in navigator.widget.observers) observer.didPush(route, previousPresent); } @@ -1829,12 +1816,10 @@ class _RouteEntry { } if (previousState == _RouteLifecycle.replace || previousState == _RouteLifecycle.pushReplace) { - RouteNotificationMessages.maybeNotifyRouteChange(_routeReplacedMethod, route, previous); for (final NavigatorObserver observer in navigator.widget.observers) observer.didReplace(newRoute: route, oldRoute: previous); } else { assert(previousState == _RouteLifecycle.push); - RouteNotificationMessages.maybeNotifyRouteChange(_routePushedMethod, route, previous); for (final NavigatorObserver observer in navigator.widget.observers) observer.didPush(route, previousPresent); } @@ -1852,11 +1837,6 @@ class _RouteEntry { currentState = _RouteLifecycle.popping; for (final NavigatorObserver observer in navigator.widget.observers) observer.didPop(route, previousPresent); - RouteNotificationMessages.maybeNotifyRouteChange( - _routePoppedMethod, - route, - previousPresent, - ); } void handleRemoval({ @required NavigatorState navigator, @required Route previousPresent }) { @@ -2009,6 +1989,8 @@ class NavigatorState extends State with TickerProviderStateMixin { yield* entry.route.overlayEntries; } + String _lastAnnouncedRouteName; + void _flushHistoryUpdates({bool rearrangeOverlay = true}) { assert(_debugLocked); // Clean up the list, sending updates to the routes that changed. Notably, @@ -2117,6 +2099,14 @@ class NavigatorState extends State with TickerProviderStateMixin { // notifications. _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; + } + // Lastly, removes the overlay entries of all marked entries and disposes // them. for (final _RouteEntry entry in toBeDisposed) { diff --git a/packages/flutter/lib/src/widgets/route_notification_messages.dart b/packages/flutter/lib/src/widgets/route_notification_messages.dart index 7bc6d6476e..abe59e92ae 100644 --- a/packages/flutter/lib/src/widgets/route_notification_messages.dart +++ b/packages/flutter/lib/src/widgets/route_notification_messages.dart @@ -5,19 +5,17 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; -import 'navigator.dart'; - /// Messages for route change notifications. class RouteNotificationMessages { - // This class is not meant to be instatiated or extended; this constructor + // This class is not meant to be instantiated or extended; this constructor // prevents instantiation and extension. // ignore: unused_element RouteNotificationMessages._(); /// When the engine is Web notify the platform for a route change. - static void maybeNotifyRouteChange(String methodName, Route route, Route previousRoute) { + static void maybeNotifyRouteChange(String routeName, String previousRouteName) { if(kIsWeb) { - _notifyRouteChange(methodName, route, previousRoute); + _notifyRouteChange(routeName, previousRouteName); } else { // No op. } @@ -25,17 +23,13 @@ class RouteNotificationMessages { /// Notifies the platform of a route change. /// - /// There are three methods: 'routePushed', 'routePopped', 'routeReplaced'. - /// /// See also: /// /// * [SystemChannels.navigation], which handles subsequent navigation /// requests. - static void _notifyRouteChange(String methodName, Route route, Route previousRoute) { - final String previousRouteName = previousRoute?.settings?.name; - final String routeName = route?.settings?.name; + static void _notifyRouteChange(String routeName, String previousRouteName) { SystemChannels.navigation.invokeMethod( - methodName, + 'routeUpdated', { 'previousRouteName': previousRouteName, 'routeName': routeName, diff --git a/packages/flutter/test/widgets/route_notification_messages_test.dart b/packages/flutter/test/widgets/route_notification_messages_test.dart index 34ae56aa01..4db5e5b135 100644 --- a/packages/flutter/test/widgets/route_notification_messages_test.dart +++ b/packages/flutter/test/widgets/route_notification_messages_test.dart @@ -63,7 +63,7 @@ void main() { expect( log.last, isMethodCall( - 'routePushed', + 'routeUpdated', arguments: { 'previousRouteName': null, 'routeName': '/', @@ -78,7 +78,7 @@ void main() { expect( log.last, isMethodCall( - 'routePushed', + 'routeUpdated', arguments: { 'previousRouteName': '/', 'routeName': '/A', @@ -93,10 +93,10 @@ void main() { expect( log.last, isMethodCall( - 'routePopped', + 'routeUpdated', arguments: { - 'previousRouteName': '/', - 'routeName': '/A', + 'previousRouteName': '/A', + 'routeName': '/', }, )); }); @@ -130,7 +130,7 @@ void main() { expect( log.last, isMethodCall( - 'routePushed', + 'routeUpdated', arguments: { 'previousRouteName': null, 'routeName': '/', @@ -145,7 +145,7 @@ void main() { expect( log.last, isMethodCall( - 'routePushed', + 'routeUpdated', arguments: { 'previousRouteName': '/', 'routeName': '/A', @@ -160,7 +160,7 @@ void main() { expect( log.last, isMethodCall( - 'routeReplaced', + 'routeUpdated', arguments: { 'previousRouteName': '/A', 'routeName': '/B', @@ -195,7 +195,7 @@ void main() { expect(log, hasLength(1)); expect( log.last, - isMethodCall('routePushed', arguments: { + isMethodCall('routeUpdated', arguments: { 'previousRouteName': null, 'routeName': '/home', }), @@ -208,7 +208,7 @@ void main() { expect(log, hasLength(2)); expect( log.last, - isMethodCall('routePushed', arguments: { + isMethodCall('routeUpdated', arguments: { 'previousRouteName': '/home', 'routeName': null, }),