Unverified Commit 7168421e authored by chunhtai's avatar chunhtai Committed by GitHub

fixes route name annoucement in navigator (#50495)

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