您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。