# fluro_decorate **Repository Path**: lazy-ai/fluro_decorate ## Basic Information - **Project Name**: fluro_decorate - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: 2.0.0 - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-10-29 - **Last Updated**: 2023-01-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # fluro_decorate Fluro 跳转封装 ## 封装简介 ### 特点 - [x] 传参清晰方便 - [x] 路由管理清晰 - [ ] 封装还不够完善 ### 使用 #### 路由表配置 ```dart void main() { NavigatorUtil.configretuin([RouterPages()], showLogStack: false); runApp(MyApp()); } ``` #### 注册路由列表 ```dart MaterialApp( onGenerateRoute: NavigatorUtil.router.generator, ) ``` #### 注册路由事件监听(使用路由缓存必须添加) ```dart MaterialApp( navigatorObservers: [//这个监听器是个集合,可根据不同需求对路由做不同的设置 NavigatorObserverDecorate(), ], ) ``` #### 页面跳转方式一 ```dart Bundle bundle = Bundle(); bundle.setRoutePage("/one"); bundle.putInt("id", 123); NavigatorUtil.push(context, bundle: bundle).then((value) { setState(() { _bundle = value; }); }); ``` #### 页面跳转方式二 ```dart /// 页面跳转并缓存当前页面 NavigatorUtil.pushNamed(context, "/twoPage", cacheCurrent: true).then((value) { print("twoPage 返回值是个啥:$value"); if (value is Bundle) { this.bundle = value; setState(() {}); } }); ``` #### 页面Pop ```dart Bundle bundle = Bundle(); bundle.putBool("bool", true); bundle.putInt("int", DateTime.now().second); bundle.putString("string", DateTime.now().toString()); NavigatorUtil.pop(context, bundle: bundle); ``` ### Bundle 主要用于传递数据;它保存的数据,是以key-value(键值对)的形式存在的。 ```dart class Bundle { /// 存储键值对 Map _map = {}; /// 存值 /// [key] 键 /// [value] 值 _setValue(String key, dynamic value) => _map[key] = value; /// 取值 /// [key] 键 _getValue(String key) { if (!containsKey(key)) { throw Exception("$key不存在"); } return _map[key]; } /// 检查当前Bundle是否包含该key值 /// [key] bool containsKey(String key) { return _map.containsKey(key); } /// 传入路由路径 /// [routePage] 路由地址 setRoutePage(String routePage) => _setValue("routePage", routePage); /// 保存bool 数据 /// [key] 键 /// [value] bool 值 putBool(String key, bool value) => _setValue(key, value); /// 获取bool数据 bool getBool(String key) => _getValue(key) as bool; /// 保存Int 数据 /// [key] 键 /// [value] int 值 putInt(String key, int value) => _setValue(key, value); /// 获取Int 数据 int getInt(String key) => _getValue(key) as int; /// ... /// Bundle默认构造函数 Bundle(); /// 带有RoutePage的Bundle构造函数 Bundle.routePage(String routePage) { setRoutePage(routePage); } @override String toString() { return _map.toString(); } } ``` #### RouterPages 实现 ```dart /// /// @ProjectName: fluro_demo /// @Package: /// @ClassName: router_pages /// @Description: dart文件作用 /// @Author: WTT /// @CreateDate: 2021/10/27 5:54 下午 class RouterPages extends RouterRegister { @override Map onInit() { Map map = {}; map["/"] = PageBuilder(builder: (bundle) { return MainPage(bundle: bundle); }); map["/one"] = PageBuilder(builder: (bundle) { int id = bundle.getInt("id"); return OnePage(id); }); map["/twoPage"] = PageBuilder(builder: (bundle) { return TwoPage(); }); map["/demoPage"] = PageBuilder(builder: (bundle) { return Demo(); }); return map; } } ``` #### PageBuilder 实现 > PageBuilder 是对 NavigatorUtil.router.define( String routePath, {@required Handler handler, TransitionType transitionType, Duration transitionDuration = const Duration(milliseconds: 250), RouteTransitionsBuilder transitionBuilder}); 的简单封装 routePath // 路由路径 handler => HandlerFunc ```dart /// /// @ProjectName: fluro_decorate /// @Package: /// @ClassName: page_builder /// @Description: dart文件作用 /// @Author: WTT /// @CreateDate: 2021/10/27 3:59 下午 typedef Widget HandlerFunc(BuildContext context, Map> params); typedef Widget PageBuilderFunc(Bundle bundle); class PageBuilder{ final PageBuilderFunc builder; HandlerFunc _handlerFunc; PageBuilder({this.builder}){ this._handlerFunc = (context, _) { return this.builder(ModalRoute.of(context).settings.arguments as Bundle); }; } /// 获取handler实例 Handler getHandler() { return Handler(handlerFunc: _handlerFunc); } } ``` ## Getting Started This project is a starting point for a Dart [package](https://gitee.com/ling213/fluro_decorate.git), a library module containing code that can be shared easily across multiple Flutter or Dart projects. For help getting started with Flutter, view our [online documentation](https://gitee.com/ling213/fluro_decorate.git), which offers tutorials, samples, guidance on mobile development, and a full API reference.