開發中常常遇到數組
解決思路:ide
main.dartui
MaterialApp(
navigatorObservers: [NavigatorManager.getInstance()],
routes: NavigatorManager.configRoutes,
...
)
複製代碼
navigator_manager.dartspa
class NavigatorManager extends NavigatorObserver {
/* 配置routes */
static Map<String, WidgetBuilder> configRoutes = {
PackageInfoPage.sName: (context) =>
SplashPage.sName: (context) => SplashPage(),
LoginPage.sName: (context) => SplashPage()),
MainPage.sName: (context) => SplashPage(),
//...
}
// 當前路由棧
static List<Route> _mRoutes;
List<Route> get routes => _mRoutes;
// 當前路由
Route get currentRoute => _mRoutes[_mRoutes.length - 1];
// stream相關
static StreamController _streamController;
StreamController get streamController=> _streamController;
// 用來路由跳轉
static NavigatorState navigator;
/* 單例給出NavigatorManager */
static NavigatorManager navigatorManager;
static NavigatorManager getInstance() {
if (navigatorManager == null) {
navigatorManager = new NavigatorManager();
_streamController = StreamController.broadcast();
}
return navigatorManager;
}
// replace 頁面
pushReplacementNamed(String routeName, [WidgetBuilder builder]) {
return navigator.pushReplacement(
CupertinoPageRoute(
builder: builder ?? configRoutes[routeName],
settings: RouteSettings(name: routeName),
),
);
}
// push 頁面
pushNamed(String routeName, [WidgetBuilder builder]) {
return navigator.push(
CupertinoPageRoute(
builder: builder ?? configRoutes[routeName],
settings: RouteSettings(name: routeName),
),
);
}
// pop 頁面
pop<T extends Object>([T result]) {
navigator.pop(result);
}
// push一個頁面, 移除該頁面下面全部頁面
pushNamedAndRemoveUntil(String newRouteName) {
return navigator.pushNamedAndRemoveUntil(newRouteName, (Route<dynamic> route) => false);
}
// 當調用Navigator.push時回調
@override
void didPush(Route route, Route previousRoute) {
super.didPush(route, previousRoute);
if (_mRoutes == null) {
_mRoutes = new List<Route>();
}
// 這裏過濾調push的是dialog的狀況
if (route is CupertinoPageRoute || route is MaterialPageRoute) {
_mRoutes.add(route);
routeObserver();
}
}
// 當調用Navigator.replace時回調
@override
void didReplace({Route newRoute, Route oldRoute}) {
super.didReplace();
if (newRoute is CupertinoPageRoute || newRoute is MaterialPageRoute) {
_mRoutes.remove(oldRoute);
_mRoutes.add(newRoute);
routeObserver();
}
}
// 當調用Navigator.pop時回調
@override
void didPop(Route route, Route previousRoute) {
super.didPop(route, previousRoute);
if (route is CupertinoPageRoute || route is MaterialPageRoute) {
_mRoutes.remove(route);
routeObserver();
}
}
@override
void didRemove(Route removedRoute, Route oldRoute) {
super.didRemove(removedRoute, oldRoute);
if (removedRoute is CupertinoPageRoute || removedRoute is MaterialPageRoute) {
_mRoutes.remove(removedRoute);
routeObserver();
}
}
void routeObserver() {
LogUtil.i(sName, '&&路由棧&&');
LogUtil.i(sName, _mRoutes);
LogUtil.i(sName, '&&當前路由&&');
LogUtil.i(sName, _mRoutes[_mRoutes.length - 1]);
// 當前頁面的navigator,用來路由跳轉
navigator = _mRoutes[_mRoutes.length - 1].navigator;
streamController.sink.add(_mRoutes);
}
}
複製代碼
token失效跳轉code
case 401:
ToastUtil.showRed('登陸失效,請從新登錄');
UserDao.clearAll();
NavigatorManager.getInstance().pushNamedAndRemoveUntil(LoginPage.sName);
break;
複製代碼
點擊push推送跳轉server
static jumpPage(String pageName, [WidgetBuilder builder]) {
String currentRouteName = NavigatorManager.getInstance().currentRoute.settings.name;
// 若是是未登陸,不跳轉
if (NavigatorManager.getInstance().routes[0].settings.name != MainPage.sName) {
return;
}
// 若是已是當前頁面就replace
if (currentRouteName == pageName) {
NavigatorManager.getInstance().pushReplacementNamed(pageName, builder);
} else {
NavigatorManager.getInstance().pushNamed(pageName, builder);
}
}
複製代碼
監聽路由改變狀態欄顏色對象
class StatusBarUtil {
static List<String> lightRouteNameList = [
TaskhallPage.sName,
//...
];
static List darkRoutNameList = [
SplashPage.sName,
LoginPage.sName,
MainPage.sName,
//...
];
static init() {
NavigatorManager.getInstance().streamController.stream.listen((state) {
setupStatusBar(state[state.length - 1]);
})
}
setupStatusBar(Route currentRoute) {
if (lightRouteNameList.contains(currentRoute.settings.name)) {
setLight();
} else if (darkRoutNameList.contains(currentRoute.settings.name)) {
setDart();
}
}
}
複製代碼