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 {
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<dynamic> previousPresent }) {
......@@ -2009,6 +1989,8 @@ class NavigatorState extends State<Navigator> 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<Navigator> 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) {
......
......@@ -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<dynamic> route, Route<dynamic> 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<dynamic> route, Route<dynamic> previousRoute) {
final String previousRouteName = previousRoute?.settings?.name;
final String routeName = route?.settings?.name;
static void _notifyRouteChange(String routeName, String previousRouteName) {
SystemChannels.navigation.invokeMethod<void>(
methodName,
'routeUpdated',
<String, dynamic>{
'previousRouteName': previousRouteName,
'routeName': routeName,
......
......@@ -63,7 +63,7 @@ void main() {
expect(
log.last,
isMethodCall(
'routePushed',
'routeUpdated',
arguments: <String, dynamic>{
'previousRouteName': null,
'routeName': '/',
......@@ -78,7 +78,7 @@ void main() {
expect(
log.last,
isMethodCall(
'routePushed',
'routeUpdated',
arguments: <String, dynamic>{
'previousRouteName': '/',
'routeName': '/A',
......@@ -93,10 +93,10 @@ void main() {
expect(
log.last,
isMethodCall(
'routePopped',
'routeUpdated',
arguments: <String, dynamic>{
'previousRouteName': '/',
'routeName': '/A',
'previousRouteName': '/A',
'routeName': '/',
},
));
});
......@@ -130,7 +130,7 @@ void main() {
expect(
log.last,
isMethodCall(
'routePushed',
'routeUpdated',
arguments: <String, dynamic>{
'previousRouteName': null,
'routeName': '/',
......@@ -145,7 +145,7 @@ void main() {
expect(
log.last,
isMethodCall(
'routePushed',
'routeUpdated',
arguments: <String, dynamic>{
'previousRouteName': '/',
'routeName': '/A',
......@@ -160,7 +160,7 @@ void main() {
expect(
log.last,
isMethodCall(
'routeReplaced',
'routeUpdated',
arguments: <String, dynamic>{
'previousRouteName': '/A',
'routeName': '/B',
......@@ -195,7 +195,7 @@ void main() {
expect(log, hasLength(1));
expect(
log.last,
isMethodCall('routePushed', arguments: <String, dynamic>{
isMethodCall('routeUpdated', arguments: <String, dynamic>{
'previousRouteName': null,
'routeName': '/home',
}),
......@@ -208,7 +208,7 @@ void main() {
expect(log, hasLength(2));
expect(
log.last,
isMethodCall('routePushed', arguments: <String, dynamic>{
isMethodCall('routeUpdated', arguments: <String, dynamic>{
'previousRouteName': '/home',
'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