Flutter路由之fluro的配置及跳转
程序员文章站
2022-03-20 14:05:50
目录1、pubspec.yaml导包,注意格式~dependencies: flutter: sdk: flutter fluro: ^1.6.32、新建路由类,改类是定义页面的路径,然后将...
1、pubspec.yaml导包,注意格式~
dependencies: flutter: sdk: flutter fluro: ^1.6.3
2、新建路由类,改类是定义页面的路径,然后将页面handler和路径设置到路由中
class routers { static router router; //文件夹须跟main.dart目录同级 static string root = '/'; static string loginpage = '/loginpage'; static string tabspage = '/tabspage'; static string messagedetailpage = '/messagedetailpage'; static string servicesettingpage = '/servicesettingpage'; static void configureroutes(router router) { router.notfoundhandler = handler( handlerfunc: (buildcontext context, map<string, list<string>> params) { print("route was not found !!!"); return null; }); router.define(loginpage, handler: loginhandler); router.define(messagedetailpage, handler: messagedetailhandler); router.define(tabspage, handler: tabshandler); router.define(servicesettingpage, handler: servicesettinghandler); } // 对参数进行encode,解决参数中有特殊字符,影响fluro路由匹配,尤其中文 static future navigateto(buildcontext context, string path, {map<string, dynamic> params, transitiontype transition = transitiontype.infromright, bool replace = false}) { string query = ""; if (params != null) { int index = 0; for (var key in params.keys) { var value = uri.encodecomponent(params[key]); if (index == 0) { query = "?"; } else { query = query + "\&"; } query += "$key=$value"; index++; } } print('我是navigateto传递的参数:$query'); path = path + query; return router.navigateto(context, path, transition: transition, replace: replace); } static void finishalltologinpage() { //跳转指定页面并关闭当前所有页面 //关闭所有页面时会导致tabs_page页面先执行initstate,再执行dispose,导致无法再监听,所以要注意 global.navkey.currentstate.pushandremoveuntil( new materialpageroute(builder: (context) => new loginpage()), (route) => route == null);//会执行所有页面的dispose } }
3、新建router_handler.dart,处理参数和跳转页面
//登录 var loginhandler = new handler( handlerfunc: (buildcontext context, map<string, list<string>> params) { return new loginpage(); }); //消息详情页 var messagedetailhandler = new handler( handlerfunc: (buildcontext context, map<string, dynamic> params) { //取参 string bartitle = params["bar_title"]?.first; string itemdatajson = params["item_data"]?.first; return new messagedetailpage( bartitle: bartitle, itemdatajson: itemdatajson, ); }); //主页tabs var tabshandler = new handler( handlerfunc: (buildcontext context, map<string, list<string>> params) { return new tabspage(); }); //service setting var servicesettinghandler = new handler( handlerfunc: (buildcontext context, map<string, list<string>> params) { return new servicesettingpage(); });
4、调用传参
//对象需要转string string itemdatajson = fluroconvertutils.object2string( _bulletinslist[index]); routers.navigateto(context, routers.messagedetailpage, params: { 'bar_title': "detail", 'item_data': itemdatajson, });
5、接收数据
//string转回对象 bulletins itemdata = bulletins.fromjson(fluroconvertutils.string2map(itemdatajson));
6、问题来了,因为fluro无法直接传中文的,这里就需要用到编码码解码了,也就是encode和decode
class fluroconvertutils { /// fluro 传递中文参数前,先转换,fluro 不支持中文传递 static string flurocnparamsencode(string originalcn) { return jsonencode(utf8encoder().convert(originalcn)); } /// fluro 传递后取出参数,解析 static string flurocnparamsdecode(string encodecn) { var list = list<int>(); ///字符串解码 jsondecode(encodecn).foreach(list.add); string value = utf8decoder().convert(list); return value; } /// string 转为 int static int string2int(string str) { return int.parse(str); } /// string 转为 double static double string2double(string str) { return double.parse(str); } /// string 转为 bool static bool string2bool(string str) { if (str == 'true') { return true; } else { return false; } } /// object 转为 string json static string object2string<t>(t t) { return flurocnparamsencode(jsonencode(t)); } /// string json 转为 map static map<string, dynamic> string2map(string str) { return json.decode(flurocnparamsdecode(str)); } }
完美解决,这也是fluro使用的整个流程,基本小封装了下,还有就是routers需要在main.dart里初始化:
myapp() { // 注册初始化fluro final router = router(); routers.configureroutes(router); routers.router = router; }
到此这篇关于flutter 路由之fluro的具体使用的文章就介绍到这了,更多相关flutter fluro内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: 如何用驱动人生系统解决扫描仪驱动问题