Commit 14e0a099 authored by ColdPaleLight's avatar ColdPaleLight

add GlobalRouteSettingsManager

parent bf030eb4
...@@ -135,7 +135,7 @@ class BoostContainerState extends NavigatorState { ...@@ -135,7 +135,7 @@ class BoostContainerState extends NavigatorState {
@override @override
BoostContainer get widget => super.widget as BoostContainer; BoostContainer get widget => super.widget as BoostContainer;
final List<Route<dynamic>> routerHistory = <Route<dynamic>>[]; List<Route<dynamic>> routerHistory = <Route<dynamic>>[];
bool multipleRouteMode = false; bool multipleRouteMode = false;
...@@ -163,7 +163,12 @@ class BoostContainerState extends NavigatorState { ...@@ -163,7 +163,12 @@ class BoostContainerState extends NavigatorState {
@override @override
void dispose() { void dispose() {
for (Route route in routerHistory) {
GlobalRouteSettingsManager.instance.removeSettings(route);
}
routerHistory.clear(); routerHistory.clear();
super.dispose(); super.dispose();
} }
...@@ -173,6 +178,8 @@ class BoostContainerState extends NavigatorState { ...@@ -173,6 +178,8 @@ class BoostContainerState extends NavigatorState {
backPressedHandler?.call(); backPressedHandler?.call();
} }
Route get topRoute => routerHistory.isNotEmpty ? routerHistory.last : null;
@override @override
Future<bool> maybePop<T extends Object>([T result]) async { Future<bool> maybePop<T extends Object>([T result]) async {
final Route<T> route = routerHistory.last; final Route<T> route = routerHistory.last;
...@@ -197,12 +204,16 @@ class BoostContainerState extends NavigatorState { ...@@ -197,12 +204,16 @@ class BoostContainerState extends NavigatorState {
@override @override
bool pop<T extends Object>([T result]) { bool pop<T extends Object>([T result]) {
Route removedRoute;
if (routerHistory.length > 1) { if (routerHistory.length > 1) {
routerHistory.removeLast(); removedRoute = routerHistory.removeLast();
} }
if (canPop()) { if (canPop()) {
super.pop<T>(result); super.pop<T>(result);
if (removedRoute != null) {
GlobalRouteSettingsManager.instance.removeSettings(removedRoute);
}
if (Platform.isIOS && multipleRouteMode && !canPop()) { if (Platform.isIOS && multipleRouteMode && !canPop()) {
FlutterBoost.singleton.channel FlutterBoost.singleton.channel
.invokeMethod<dynamic>('enablePopGesture', null); .invokeMethod<dynamic>('enablePopGesture', null);
...@@ -329,3 +340,28 @@ class ContainerNavigatorObserver extends NavigatorObserver { ...@@ -329,3 +340,28 @@ class ContainerNavigatorObserver extends NavigatorObserver {
} }
} }
} }
class GlobalRouteSettingsManager {
GlobalRouteSettingsManager._();
static GlobalRouteSettingsManager instance = GlobalRouteSettingsManager._();
final Map<Route,BoostRouteSettings> _routeSettingsMap = <Route,BoostRouteSettings>{};
void addSettings(Route route,BoostRouteSettings settings) {
_routeSettingsMap[route] = settings;
}
void removeSettings(Route route) {
_routeSettingsMap.remove(route);
}
BoostRouteSettings getSettings(Route route) {
return _routeSettingsMap[route];
}
bool contains(Route route) {
return _routeSettingsMap[route] != null;
}
}
...@@ -51,8 +51,6 @@ typedef void PostPushRoute(String url, String uniqueId, Map params, Route route, ...@@ -51,8 +51,6 @@ typedef void PostPushRoute(String url, String uniqueId, Map params, Route route,
typedef Route FlutterBoostRouteBuilder(Widget widget); typedef Route FlutterBoostRouteBuilder(Widget widget);
class FlutterBoost { class FlutterBoost {
static final FlutterBoost _instance = FlutterBoost(); static final FlutterBoost _instance = FlutterBoost();
final GlobalKey<ContainerManagerState> containerManagerKey = final GlobalKey<ContainerManagerState> containerManagerKey =
...@@ -155,39 +153,40 @@ class FlutterBoost { ...@@ -155,39 +153,40 @@ class FlutterBoost {
{Map<String, dynamic> urlParams, {Map<String, dynamic> urlParams,
Map<String, dynamic> exts, Map<String, dynamic> exts,
FlutterBoostRouteBuilder routeBuilder}) { FlutterBoostRouteBuilder routeBuilder}) {
final BoostRouteSettings routeSettings = ContainerCoordinator.singleton
.createRouteSettings(url, urlParams: urlParams, exts: exts);
final BoostRouteSettings routeSettings = ContainerCoordinator.singleton.createRouteSettings(url,urlParams: urlParams,exts: exts);
final Widget page = ContainerCoordinator.singleton.createPage( final Widget page = ContainerCoordinator.singleton.createPage(
routeSettings.name, routeSettings.params, routeSettings.uniqueId); routeSettings.name, routeSettings.params, routeSettings.uniqueId);
if (page == null ) { if (page == null) {
return open(url, urlParams: urlParams, exts: exts); return open(url, urlParams: urlParams, exts: exts);
} }
final Route<Map<dynamic, dynamic>> route = routeBuilder != null final Route<Map<dynamic, dynamic>> route = routeBuilder != null
? routeBuilder(page) ? routeBuilder(page)
: defaultRoute(page,routeSettings); : defaultRoute(page, routeSettings);
FlutterBoost.containerManager?.onstageContainer?.multipleRouteMode = true; GlobalRouteSettingsManager.instance.addSettings(route, routeSettings);
FlutterBoost.containerManager?.onstageContainer?.multipleRouteMode = true;
return FlutterBoost.containerManager?.onstageContainer?.push(route); return FlutterBoost.containerManager?.onstageContainer?.push(route);
} }
Route<Map<dynamic, dynamic>> defaultRoute(Widget page,BoostRouteSettings settings ) { Route<Map<dynamic, dynamic>> defaultRoute(Widget page,
RouteSettings routeSettings =new RouteSettings(name:settings.name,arguments:settings.params); BoostRouteSettings settings) {
RouteSettings routeSettings = new RouteSettings(
name: settings.name, arguments: settings.params);
if (Platform.isIOS) { if (Platform.isIOS) {
return CupertinoPageRoute<Map<dynamic, dynamic>> ( return CupertinoPageRoute<Map<dynamic, dynamic>>(
settings: routeSettings, settings: routeSettings,
builder: (BuildContext context) => page builder: (BuildContext context) => page
); );
} }
return PageRouteBuilder( return PageRouteBuilder(
transitionDuration: Duration(milliseconds: 300), transitionDuration: Duration(milliseconds: 300),
settings:routeSettings , settings: routeSettings,
pageBuilder: (context, animation, secondaryAnimation) => page, pageBuilder: (context, animation, secondaryAnimation) => page,
transitionsBuilder: (context, animation, secondaryAnimation, child) { transitionsBuilder: (context, animation, secondaryAnimation, child) {
var offsetAnimation = Tween<Offset>( var offsetAnimation = Tween<Offset>(
...@@ -199,7 +198,6 @@ class FlutterBoost { ...@@ -199,7 +198,6 @@ class FlutterBoost {
child: child, child: child,
); );
}); });
} }
/** /**
...@@ -211,9 +209,9 @@ class FlutterBoost { ...@@ -211,9 +209,9 @@ class FlutterBoost {
Future<bool> close(String id, Future<bool> close(String id,
{Map<String, dynamic> result, Map<String, dynamic> exts}) { {Map<String, dynamic> result, Map<String, dynamic> exts}) {
//判断当前onStage的容器是不是通过openInCurrentContainer打开过界面 //判断当前onStage的容器是不是通过openInCurrentContainer打开过界面
if (FlutterBoost.containerManager?.onstageContainer?.multipleRouteMode ?? false) { if (FlutterBoost.containerManager?.onstageContainer?.multipleRouteMode ??
false) {
return Future.value(closeInCurrentContainer(result)); return Future.value(closeInCurrentContainer(result));
} }
...@@ -247,11 +245,8 @@ class FlutterBoost { ...@@ -247,11 +245,8 @@ class FlutterBoost {
properties["exts"] = exts; properties["exts"] = exts;
} }
return channel.invokeMethod<bool>('closePage', properties); return channel.invokeMethod<bool>('closePage', properties);
} }
Future<bool> closeCurrent( Future<bool> closeCurrent(
{Map<String, dynamic> result, Map<String, dynamic> exts}) { {Map<String, dynamic> result, Map<String, dynamic> exts}) {
BoostContainerSettings settings = containerManager?.onstageSettings; BoostContainerSettings settings = containerManager?.onstageSettings;
...@@ -290,4 +285,16 @@ class FlutterBoost { ...@@ -290,4 +285,16 @@ class FlutterBoost {
///register callbacks for Navigators push & pop ///register callbacks for Navigators push & pop
void addBoostNavigatorObserver(NavigatorObserver observer) => void addBoostNavigatorObserver(NavigatorObserver observer) =>
ContainerNavigatorObserver.boostObservers.add(observer); ContainerNavigatorObserver.boostObservers.add(observer);
BoostRouteSettings getBoostRouteSettings(Route route) {
return GlobalRouteSettingsManager.instance.getSettings(route);
}
BoostRouteSettings getCurrentBoostRouteSettings() {
Route route = FlutterBoost?.containerManager?.onstageContainer?.topRoute;
if (route != null) {
return GlobalRouteSettingsManager.instance.getSettings(route);
}
return 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