flutter導(dǎo)航fluro

flutter導(dǎo)航框架fluro https://github.com/lukepighetti/fluro

前一章節(jié)介紹了Navigator的導(dǎo)航跳轉(zhuǎn)荸实,今天我們來看看fluro框架提供的跳轉(zhuǎn)轮洋。flutter提供的Navigator和fluro這兩個到底哪個好用罚攀,個人推薦使用fluro掸哑。廢話不多說局扶,一個字干谬哀。

1项戴、在pubspec.yaml中添加引用
fluro: ^1.7.8
執(zhí)行一下
flutter pub get
2宛裕、基礎(chǔ)配置

class BaseRouter{

  static FluroRouter _mFluroRouter;

  static FluroRouter getRouter(){
    return _mFluroRouter;
  }

  static void setRouter(FluroRouter router){
    _mFluroRouter = router;
  }

  static List<IRouter> _mListRouter = [];
  static void registerConfigureRoutes(FluroRouter router){
      if(router == null){
        throw Exception("fluroRouter is null, please init router");
      }

      router.notFoundHandler = Handler(
        handlerFunc:(BuildContext context, Map<String, List<String>> parameters){
          print("頁面沒有注冊穆碎,找不到該頁面  ");
          return RouteNotFound();
        }
      );

      _mListRouter.clear();
      //添加模塊路由
      _mListRouter.add(LoginRouter());

      _mListRouter.forEach((element) {
        element.initFluroRouter(router);
      });
  }

}

3牙勘、定義模塊路由注冊

abstract class IRouter {
  void initFluroRouter(FluroRouter fluroRouter);
}

4、模塊路由配置

class LoginRouter extends IRouter{


   static String loginPage = "/login/loginPage";
   static String loginUserInfoPage = "/login/loginUserInfoPage";

  @override
  void initFluroRouter(FluroRouter fluroRouter) {
    // TODO: implement initFluroRouter
    fluroRouter.define(loginPage, handler: Handler(handlerFunc: (_,params){
        String userName = params[LoginPage.bundleKeyUserName]?.first;
        String times = params[LoginPage.bundleKeyTime]?.first;
        return LoginPage(userName,times);
    }));

    fluroRouter.define(loginUserInfoPage, handler: Handler(handlerFunc: (context,params){
      final args = context.settings.arguments as UserInfo;
      return LoginInfoPage(args);
    }));
  }

}

5所禀、統(tǒng)一跳轉(zhuǎn)配置

class NavigatorUtils {
  static void push(BuildContext context, String path,
      {bool replace = false, bool clearStack = false}) {
    FocusScope.of(context).unfocus();
    BaseRouter.getRouter().navigateTo(context, path,
        replace: replace,
        clearStack: clearStack,
        transition: TransitionType.native);
  }

  static void pushResult(
      BuildContext context, String path, Function(Object) function,
      {bool replace = false, bool clearStack = false}) {
    FocusScope.of(context).unfocus();
    BaseRouter.getRouter()
        .navigateTo(context, path,
            replace: replace,
            clearStack: clearStack,
            transition: TransitionType.native)
        .then((value) {
      if (value == null) {
        return;
      }
      function(value);
    }).catchError((onError) {
      print("$onError");
    });
  }

  static void pushArgumentResult(BuildContext context, String path,
      Object argument, Function(Object) function,
      {bool replace = false, bool clearStack = false}) {
    BaseRouter.getRouter()
        .navigateTo(context, path,
            routeSettings: RouteSettings(arguments: argument), replace: replace, clearStack: clearStack)
        .then((value) {
      if (value == null) {
        return;
      }
      function(value);
    }).catchError((onError) {
      print("$onError");
    });
  }

  static void pushArgument(
      BuildContext context, String path, Object argument,
      {bool replace = false, bool clearStack = false}) {
    BaseRouter.getRouter().navigateTo(context, path,
        routeSettings: RouteSettings(arguments: argument), replace: replace, clearStack: clearStack);
  }

  static void goBack(BuildContext context) {
    FocusScope.of(context).unfocus();
    Navigator.pop(context);
  }

  static void goBackWithParams(BuildContext context, result) {
    FocusScope.of(context).unfocus();
    Navigator.pop(context, result);
  }

  static String changeToNavigatorPath(String registerPath,
      {Map<String, Object> params}) {
    if (params == null || params.isEmpty) {
      return registerPath;
    }
    StringBuffer bufferStr = StringBuffer();
    params.forEach((key, value) {
      bufferStr
        ..write(key)
        ..write("=")
        ..write(Uri.encodeComponent(value))
        ..write("&");
    });
    String paramStr = bufferStr.toString();
    paramStr = paramStr.substring(0, paramStr.length - 1);
    print("傳遞的參數(shù)  $paramStr");
    return "$registerPath?$paramStr";
  }
}

路由跳轉(zhuǎn)

NavigatorUtils.push(context, "/login/loginPage?userName=flutterName&email=123@qq.com");

//path的配置 /login/loginPage?userName=flutterName&email=123@qq.com

path就像http中g(shù)et請求的url鏈接方面,"?"號前的是fluroRouter.define()中的routePath字段,后面的就是我們要傳遞的參數(shù)色徘。

參數(shù)獲取

fluroRouter.define("/login/loginPage", handler: Handler(handlerFunc: (_,params){
       String userName = params["userName"]?.first;
       String email = params["email"]?.first;
       return LoginPage(userName,email);
   }));

這樣傳遞的參數(shù)只能是字符串格式恭金,如果字符串中包含中文就需要使用Uri.encodeComponent進(jìn)行轉(zhuǎn)義

其他類型參數(shù)傳遞

fluroRouter.define(loginUserInfoPage, handler: Handler(handlerFunc: (context,params){
      final args = context.settings.arguments as UserInfo;
      return LoginInfoPage(args);
    }));

UserInfo userInfo = UserInfo();
    userInfo.email = "xiao@163.com";
    userInfo.name = "小小";
    NavigatorUtils.pushArgument(context, LoginRouter.loginUserInfoPage, userInfo);

static void pushArgument(
      BuildContext context, String path, Object argument,
      {bool replace = false, bool clearStack = false}) {
    BaseRouter.getRouter().navigateTo(context, path,
        routeSettings: RouteSettings(arguments: argument), replace: replace, clearStack: clearStack);
  }

其他的類型參數(shù)直接在routeSettings中設(shè)置,其他的都是一樣

Demo地址: https://github.com/yangyang10/fluroDemo/tree/main/router_by_fluro

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末褂策,一起剝皮案震驚了整個濱河市横腿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌斤寂,老刑警劉巖耿焊,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異遍搞,居然都是意外死亡罗侯,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門溪猿,熙熙樓的掌柜王于貴愁眉苦臉地迎上來钩杰,“玉大人,你說我怎么就攤上這事诊县〗才” “怎么了?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵依痊,是天一觀的道長避除。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么驹饺? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任钳枕,我火速辦了婚禮,結(jié)果婚禮上赏壹,老公的妹妹穿的比我還像新娘鱼炒。我一直安慰自己,他們只是感情好蝌借,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布昔瞧。 她就那樣靜靜地躺著,像睡著了一般菩佑。 火紅的嫁衣襯著肌膚如雪自晰。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機(jī)與錄音混巧,去河邊找鬼勤揩。 笑死咧党,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蛙埂。 我是一名探鬼主播箱残,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼敬惦,長吁一口氣:“原來是場噩夢啊……” “哼俄删!你這毒婦竟也來了臊诊?” 一聲冷哼從身側(cè)響起抓艳,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤片任,失蹤者是張志新(化名)和其女友劉穎对供,沒想到半個月后产场,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體迈勋,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡米愿,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了博烂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片禽篱。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡玛界,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出笨枯,到底是詐尸還是另有隱情,我是刑警寧澤硫嘶,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布哮塞,位于F島的核電站忆畅,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏绊诲。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一褪贵、第九天 我趴在偏房一處隱蔽的房頂上張望掂之。 院中可真熱鬧脆丁,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春瘦锹,著一層夾襖步出監(jiān)牢的瞬間籍嘹,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工辱士, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留颂碘,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓椅挣,卻偏偏與公主長得像头岔,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子贴妻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容