您好,登錄后才能下訂單哦!
1. 應用場景
開發中經常遇到
2. 解決方案
解決思路:
3. 具體實現
main.dart
MaterialApp( navigatorObservers: [NavigatorManager.getInstance()], routes: NavigatorManager.configRoutes, ... )
navigator_manager.dart
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); } }
4. 如何使用
token失效跳轉
case 401: ToastUtil.showRed('登錄失效,請重新登陸'); UserDao.clearAll(); NavigatorManager.getInstance().pushNamedAndRemoveUntil(LoginPage.sName); break;
點擊push推送跳轉
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(); } } }
完結,撒花🎉
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。