Flutter 路由跳转 fluro

地址:https://pub.flutter-io.cn/packages/fluro

 

dependencies:
  fluro: ^2.0.4
Routes管理类
import 'package:common_lib/fluro/router_handler.dart';
import 'package:fluro/fluro.dart';


/**
 * 路由管理
 */
class Routes {
  // static Router router;
  
  // 根路径
  static String root = "/";
  // 设置页面
  static String settings = "/setting";
  // 网页加载
  static String webView = "/webView";

  // 配置route
  static void configureRoutes(FluroRouter router) {
    // 未发现对应route 处理
    router.notFoundHandler = RouterHandler.notFoundHandler;
    // router.define(root, handler: handler)
    router.define(settings, handler: RouterHandler.settingHandler);
    router.define(webView, handler: RouterHandler.webViewHandler);
  }

}

 handler管理类 router_handler

import 'package:common_lib/pages/NotFoundPage.dart';
import 'package:common_lib/pages/home_page.dart';
import 'package:common_lib/pages/setting_page.dart';
import 'package:common_lib/pages/webview_page.dart';
import 'package:fluro/fluro.dart';

class RouterHandler {
  // 根路径
 static var rootHandler = Handler(handlerFunc: (context,params) {
    return HomePage();
  });

// 页面不存在
  static var notFoundHandler = Handler(handlerFunc: (context,params) {
    return NotFoundPage();
  });

// 设置页面  不带参数
  static var settingHandler = Handler(handlerFunc: (context,params) {
    return SettingPage();
  });

  // 网页加载带参数 -示例:传多个字符串参数
  static var webViewHandler = Handler(handlerFunc: (context,params) {
    // params 内容为 {title: [我是标题哈哈哈],url: [https://www.baidu.com]}
    String? titleStr = params['title']?.first;
    String? urlStr = params['url']?.first;
    return WebViewPage(title: "$titleStr", url: "$urlStr");
  });
}

 配置router

MyApp() {
   // 注册初始化fluro
   final router = Router();
   Routers.configureRoutes(router);
   Routers.router = router;
 }

 跳转

//对象需要转String
                     String itemDataJson = FluroConvertUtils.object2string(
                         _bulletinsList[index]);
                     Routers.navigateTo(context, Routers.messageDetailPage,
                         params: {
                           'bar_title': "Detail",
                           'item_data': itemDataJson,
                         });

 

 



 

posted @ 2022-11-22 16:07  疯子FK  阅读(146)  评论(0编辑  收藏  举报