Commit fa865f26 authored by yangwu.jia's avatar yangwu.jia

NavObserver bugfixed

parent 12f38de5
...@@ -29,6 +29,7 @@ class _MyAppState extends State<MyApp> { ...@@ -29,6 +29,7 @@ class _MyAppState extends State<MyApp> {
return FlutterRouteWidget(params:params); return FlutterRouteWidget(params:params);
}, },
}); });
FlutterBoost.singleton.addBoostNavigatorObserver(TestBoostNavigatorObserver());
} }
@override @override
...@@ -43,3 +44,22 @@ class _MyAppState extends State<MyApp> { ...@@ -43,3 +44,22 @@ class _MyAppState extends State<MyApp> {
String pageName, String uniqueId, Map params, Route route, Future _) { String pageName, String uniqueId, Map params, Route route, Future _) {
} }
} }
class TestBoostNavigatorObserver extends NavigatorObserver{
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
print("flutterboost#didPush");
}
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
print("flutterboost#didPop");
}
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
print("flutterboost#didRemove");
}
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
print("flutterboost#didReplace");
}
}
...@@ -155,14 +155,11 @@ class BoostContainerState extends NavigatorState { ...@@ -155,14 +155,11 @@ class BoostContainerState extends NavigatorState {
@override @override
void didUpdateWidget(Navigator oldWidget) { void didUpdateWidget(Navigator oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
findContainerNavigatorObserver(oldWidget)?.removeBoostNavigatorObserver(
FlutterBoost.containerManager.navigatorObserver);
} }
@override @override
void dispose() { void dispose() {
findContainerNavigatorObserver(widget)?.removeBoostNavigatorObserver(
FlutterBoost.containerManager.navigatorObserver);
routerHistory.clear(); routerHistory.clear();
super.dispose(); super.dispose();
} }
...@@ -261,63 +258,52 @@ class ContainerElement extends StatefulElement { ...@@ -261,63 +258,52 @@ class ContainerElement extends StatefulElement {
} }
class ContainerNavigatorObserver extends NavigatorObserver { class ContainerNavigatorObserver extends NavigatorObserver {
BoostNavigatorObserver observer;
final Set<BoostNavigatorObserver> _boostObservers = static final Set<NavigatorObserver> boostObservers =
Set<BoostNavigatorObserver>(); Set<NavigatorObserver>();
ContainerNavigatorObserver(); ContainerNavigatorObserver();
factory ContainerNavigatorObserver.bindContainerManager() => factory ContainerNavigatorObserver.bindContainerManager() =>
ContainerNavigatorObserver() ContainerNavigatorObserver();
..addBoostNavigatorObserver(
FlutterBoost.containerManager.navigatorObserver);
VoidCallback addBoostNavigatorObserver(BoostNavigatorObserver observer) { VoidCallback addBoostNavigatorObserver(NavigatorObserver observer) {
_boostObservers.add(observer); boostObservers.add(observer);
return () => _boostObservers.remove(observer); return () => boostObservers.remove(observer);
} }
void removeBoostNavigatorObserver(BoostNavigatorObserver observer) { void removeBoostNavigatorObserver(NavigatorObserver observer) {
_boostObservers.remove(observer); boostObservers.remove(observer);
} }
@override @override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) { void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
for (BoostNavigatorObserver observer in _boostObservers) { for (NavigatorObserver observer in boostObservers) {
observer.didPush(route, previousRoute); observer.didPush(route, previousRoute);
} }
} }
@override @override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) { void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
for (BoostNavigatorObserver observer in _boostObservers) { for (NavigatorObserver observer in boostObservers) {
observer.didPop(route, previousRoute); observer.didPop(route, previousRoute);
} }
} }
@override @override
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) { void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
for (BoostNavigatorObserver observer in _boostObservers) { for (NavigatorObserver observer in boostObservers) {
observer.didRemove(route, previousRoute); observer.didRemove(route, previousRoute);
} }
} }
@override @override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) { void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
for (BoostNavigatorObserver observer in _boostObservers) { for (NavigatorObserver observer in boostObservers) {
observer.didReplace(newRoute: newRoute, oldRoute: oldRoute); observer.didReplace(newRoute: newRoute, oldRoute: oldRoute);
} }
} }
} }
class BoostNavigatorObserver {
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {}
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {}
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {}
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {}
}
...@@ -62,8 +62,6 @@ class BoostContainerManager extends StatefulWidget { ...@@ -62,8 +62,6 @@ class BoostContainerManager extends StatefulWidget {
class ContainerManagerState extends State<BoostContainerManager> { class ContainerManagerState extends State<BoostContainerManager> {
final GlobalKey<OverlayState> _overlayKey = GlobalKey<OverlayState>(); final GlobalKey<OverlayState> _overlayKey = GlobalKey<OverlayState>();
final List<BoostContainer> _offstage = <BoostContainer>[]; final List<BoostContainer> _offstage = <BoostContainer>[];
final ManagerNavigatorObserver _navigatorObserver =
ManagerNavigatorObserver();
List<_ContainerOverlayEntry> _leastEntries; List<_ContainerOverlayEntry> _leastEntries;
...@@ -78,7 +76,6 @@ class ContainerManagerState extends State<BoostContainerManager> { ...@@ -78,7 +76,6 @@ class ContainerManagerState extends State<BoostContainerManager> {
bool get foreground => _foreground; bool get foreground => _foreground;
ManagerNavigatorObserver get navigatorObserver => _navigatorObserver;
//Number of containers. //Number of containers.
int get containerCounts => _offstage.length; int get containerCounts => _offstage.length;
...@@ -347,51 +344,3 @@ class _ContainerOverlayEntry extends OverlayEntry { ...@@ -347,51 +344,3 @@ class _ContainerOverlayEntry extends OverlayEntry {
} }
} }
class ManagerNavigatorObserver extends BoostNavigatorObserver {
BoostNavigatorObserver observer;
final Set<BoostNavigatorObserver> _boostObservers =
Set<BoostNavigatorObserver>();
VoidCallback addBoostNavigatorObserver(BoostNavigatorObserver observer) {
_boostObservers.add(observer);
return () => _boostObservers.remove(observer);
}
void removeBoostNavigatorObserver(BoostNavigatorObserver observer) {
_boostObservers.remove(observer);
}
@override
void didPush(Route<dynamic> route, Route<dynamic> previousRoute) {
Logger.log('ManagerNavigatorObserver didPush');
for (BoostNavigatorObserver observer in _boostObservers) {
observer.didPush(route, previousRoute);
}
}
@override
void didPop(Route<dynamic> route, Route<dynamic> previousRoute) {
Logger.log('ManagerNavigatorObserver didPop');
for (BoostNavigatorObserver observer in _boostObservers) {
observer.didPop(route, previousRoute);
}
}
@override
void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) {
Logger.log('ManagerNavigatorObserver didRemove');
for (BoostNavigatorObserver observer in _boostObservers) {
observer.didRemove(route, previousRoute);
}
}
@override
void didReplace({Route<dynamic> newRoute, Route<dynamic> oldRoute}) {
Logger.log('ManagerNavigatorObserver didReplace');
for (BoostNavigatorObserver observer in _boostObservers) {
observer.didReplace(newRoute: newRoute, oldRoute: oldRoute);
}
}
}
...@@ -189,8 +189,8 @@ class FlutterBoost { ...@@ -189,8 +189,8 @@ class FlutterBoost {
_observersHolder.addObserver<BoostContainerLifeCycleObserver>(observer); _observersHolder.addObserver<BoostContainerLifeCycleObserver>(observer);
///register callbacks for Navigators push & pop ///register callbacks for Navigators push & pop
VoidCallback addBoostNavigatorObserver(BoostNavigatorObserver observer) => void addBoostNavigatorObserver(NavigatorObserver observer) =>
_observersHolder.addObserver<BoostNavigatorObserver>(observer); ContainerNavigatorObserver.boostObservers.add(observer);
} }
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