1. 應用場景
我們一直強調網站設計、成都網站制作對于企業(yè)的重要性,如果您也覺得重要,那么就需要我們慎重對待,選擇一個安全靠譜的網站建設公司,企業(yè)網站我們建議是要么不做,要么就做好,讓網站能真正成為企業(yè)發(fā)展過程中的有力推手。專業(yè)網站制作公司不一定是大公司,成都創(chuàng)新互聯公司作為專業(yè)的網絡公司選擇我們就是放心。
開發(fā)中經常遇到
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); } }
監(jiān)聽路由改變狀態(tài)欄顏色
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(); } } }
完結,撒花🎉
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對創(chuàng)新互聯的支持。
本文題目:Flutter上線項目實戰(zhàn)記錄之路由篇
本文網址:http://muchs.cn/article38/gddspp.html
成都網站建設公司_創(chuàng)新互聯,為您提供靜態(tài)網站、虛擬主機、電子商務、關鍵詞優(yōu)化、網站建設、網站策劃
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯