您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Flutter路由之fluro如何配置及跳轉(zhuǎn),小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
dependencies: flutter: sdk: flutter fluro: ^1.6.3
class Routers { static Router router; //文件夾須跟main.dart目錄同級(jí) 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); } // 對(duì)參數(shù)進(jìn)行encode,解決參數(shù)中有特殊字符,影響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傳遞的參數(shù):$query'); path = path + query; return router.navigateTo(context, path, transition: transition, replace: replace); } static void finishAllToLoginPage() { //跳轉(zhuǎn)指定頁(yè)面并關(guān)閉當(dāng)前所有頁(yè)面 //關(guān)閉所有頁(yè)面時(shí)會(huì)導(dǎo)致tabs_page頁(yè)面先執(zhí)行initState,再執(zhí)行dispose,導(dǎo)致無(wú)法再監(jiān)聽(tīng),所以要注意 Global.navKey.currentState.pushAndRemoveUntil( new MaterialPageRoute(builder: (context) => new LoginPage()), (route) => route == null);//會(huì)執(zhí)行所有頁(yè)面的dispose } }
//登錄 var loginHandler = new Handler( handlerFunc: (BuildContext context, Map<String, List<String>> params) { return new LoginPage(); }); //消息詳情頁(yè) 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, ); }); //主頁(yè)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(); });
//對(duì)象需要轉(zhuǎn)String String itemDataJson = FluroConvertUtils.object2string( _bulletinsList[index]); Routers.navigateTo(context, Routers.messageDetailPage, params: { 'bar_title': "Detail", 'item_data': itemDataJson, });
//String轉(zhuǎn)回對(duì)象 Bulletins itemData = Bulletins.fromJson(FluroConvertUtils.string2map(itemDataJson));
class FluroConvertUtils { /// fluro 傳遞中文參數(shù)前,先轉(zhuǎn)換,fluro 不支持中文傳遞 static String fluroCnParamsEncode(String originalCn) { return jsonEncode(Utf8Encoder().convert(originalCn)); } /// fluro 傳遞后取出參數(shù),解析 static String fluroCnParamsDecode(String encodeCn) { var list = List<int>(); ///字符串解碼 jsonDecode(encodeCn).forEach(list.add); String value = Utf8Decoder().convert(list); return value; } /// string 轉(zhuǎn)為 int static int string2int(String str) { return int.parse(str); } /// string 轉(zhuǎn)為 double static double string2double(String str) { return double.parse(str); } /// string 轉(zhuǎn)為 bool static bool string2bool(String str) { if (str == 'true') { return true; } else { return false; } } /// object 轉(zhuǎn)為 string json static String object2string<T>(T t) { return fluroCnParamsEncode(jsonEncode(t)); } /// string json 轉(zhuǎn)為 map static Map<String, dynamic> string2map(String str) { return json.decode(fluroCnParamsDecode(str)); } }
完美解決,這也是fluro使用的整個(gè)流程,基本小封裝了下,還有就是Routers需要在main.dart里初始化:
MyApp() { // 注冊(cè)初始化fluro final router = Router(); Routers.configureRoutes(router); Routers.router = router; }
關(guān)于“Flutter路由之fluro如何配置及跳轉(zhuǎn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。