Fluro 是一款优秀的 Flutter 路由框架。

使用

1、pubspec.yaml中添加引用

  1. fluro: ^2.0.3

执行命令:

  1. flutter pub get

2、定义模块路由注册

  1. abstract class IRouter {
  2. void initFluroRouter(FluroRouter fluroRouter);
  3. }

3、基本配置

  1. class BaseRouter {
  2. static FluroRouter? _mFluroRouter;
  3. static FluroRouter? getRouter() {
  4. return _mFluroRouter;
  5. }
  6. static void setRouter(FluroRouter router) {
  7. _mFluroRouter = router;
  8. }
  9. static List<IRouter> _mListRouter = [];
  10. static void registerConfigureRoutes(FluroRouter? router) {
  11. if (router == null) {
  12. throw Exception("fluroRouter is null, please init router");
  13. }
  14. router.notFoundHandler = Handler(
  15. handlerFunc:
  16. (BuildContext? context, Map<String, List<String>> parameters) {
  17. print("页面没有注册,找不到该页面 ");
  18. },
  19. );
  20. _mListRouter.clear();
  21. //添加路由配置
  22. _mListRouter.add(PageRouter());
  23. _mListRouter.forEach((element) {
  24. element.initFluroRouter(router);
  25. });
  26. }
  27. }

4、模块路由配置

  1. class PageRouter extends IRouter {
  2. static String noParamPage = "page/null";
  3. static String paramsPathPage = "page/params/path";
  4. static String paramsQueryPage = "page/params/query";
  5. static String paramsObjectPage = "page/params/object";
  6. @override
  7. void initFluroRouter(FluroRouter fluroRouter) {
  8. fluroRouter.define(noParamPage,
  9. handler: Handler(handlerFunc: (_, __) => NoParamPage()));
  10. fluroRouter.define("$paramsPathPage/:k1",
  11. handler: Handler(handlerFunc: (_, params) {
  12. String? p1 = params[ParamsPage.k1]?.first;
  13. return ParamsPage(p1: p1);
  14. }));
  15. fluroRouter.define("$paramsPathPage/:k1/:k2",
  16. handler: Handler(handlerFunc: (_, params) {
  17. String? p1 = params[ParamsPage.k1]?.first;
  18. String? p2 = params[ParamsPage.k2]?.first;
  19. return ParamsPage(p1: p1, p2: p2);
  20. }));
  21. fluroRouter.define("$paramsPathPage/:k1/:k2/:k3",
  22. handler: Handler(handlerFunc: (_, params) {
  23. String? p1 = params[ParamsPage.k1]?.first;
  24. String? p2 = params[ParamsPage.k2]?.first;
  25. String? p3 = params[ParamsPage.k3]?.first;
  26. return ParamsPage(p1: p1, p2: p2, p3: p3);
  27. }));
  28. fluroRouter.define(paramsQueryPage, handler: Handler(
  29. handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
  30. String? p1 = params[ParamsPage.k1]?.first;
  31. String? p2 = params[ParamsPage.k2]?.first;
  32. String? p3 = params[ParamsPage.k3]?.first;
  33. return ParamsPage(p1: p1, p2: p2, p3: p3);
  34. }));
  35. // 对象参数
  36. fluroRouter.define(paramsObjectPage, handler: Handler(handlerFunc:
  37. (BuildContext? context, Map<String, List<String>> parameters) {
  38. var model = context?.settings?.arguments;
  39. if (model != null && model is InfoModel) {
  40. return ObjectPage(userInfo: model);
  41. }
  42. return ObjectPage();
  43. }));
  44. }
  45. }

5、统一跳转控制

  1. class NavigatorUtils {
  2. static Future<dynamic> push(
  3. BuildContext context,
  4. String path, {
  5. Map<String, Object>? params,
  6. Object? argument,
  7. bool replace = false,
  8. bool clearStack = false,
  9. }) async {
  10. FocusScope.of(context).unfocus();
  11. if (params?.isNotEmpty ?? false) {
  12. return push(context, _params2Path(path, params: params),
  13. replace: replace, clearStack: clearStack);
  14. }
  15. return await BaseRouter.getRouter()
  16. ?.navigateTo(context, path,
  17. routeSettings:
  18. argument == null ? null : RouteSettings(arguments: argument),
  19. replace: replace,
  20. clearStack: clearStack,
  21. transition: TransitionType.native)
  22. .catchError((onError) {
  23. print("$onError");
  24. });
  25. }
  26. static void goBack(BuildContext context) {
  27. FocusScope.of(context).unfocus();
  28. Navigator.pop(context);
  29. }
  30. static void goBackWithParams(BuildContext context, result) {
  31. FocusScope.of(context).unfocus();
  32. Navigator.pop(context, result);
  33. }
  34. static String _params2Path(String path, {Map<String, Object>? params}) {
  35. if (params == null || params.isEmpty) {
  36. return path;
  37. }
  38. StringBuffer bufferStr = StringBuffer();
  39. params.forEach((key, value) {
  40. bufferStr
  41. ..write("&")
  42. ..write(key)
  43. ..write('=')
  44. ..write(Uri.encodeComponent(value.toString()));
  45. });
  46. return "$path?${bufferStr.toString().substring(1)}";
  47. }
  48. }

6、路由跳转

  1. // 无参数
  2. NavigatorUtils.push(context, PageRouter.noParamPage);
  3. // 一个参数(path)
  4. NavigatorUtils.push(context, "${PageRouter.paramsPathPage}/口十耳");
  5. // 多个参数(path)
  6. NavigatorUtils.push(context, "${PageRouter.paramsPathPage}/口十耳/12/2021-08-04 12:12:12");
  7. // 多参数(key=value)
  8. NavigatorUtils.push(context, PageRouter.paramsQueryPage,
  9. params: {
  10. ParamsPage.k1: "口十耳",
  11. ParamsPage.k3: 12,
  12. ParamsPage.k2: DateTime.now(),
  13. });
  14. // 复杂对象入参
  15. NavigatorUtils.push(
  16. context,
  17. PageRouter.paramsObjectPage,
  18. argument: InfoModel(
  19. name: "JsonYe",
  20. age: 100,
  21. sex: true,
  22. ),
  23. );

7、获取参数

7.1 路径获取方式

  1. fluroRouter.define("$paramsPathPage/:k1/:k2/:k3",
  2. handler: Handler(handlerFunc: (_, params) {
  3. String? p1 = params[ParamsPage.k1]?.first;
  4. String? p2 = params[ParamsPage.k2]?.first;
  5. String? p3 = params[ParamsPage.k3]?.first;
  6. return ParamsPage(p1: p1, p2: p2, p3: p3);
  7. }));

这样传递的参数只能是字符串格式,如果字符串中包含中文就需要使用Uri.encodeComponent进行转义

7.2、键-值 对方式

  1. fluroRouter.define(paramsQueryPage, handler: Handler(
  2. handlerFunc: (BuildContext? context, Map<String, List<String>> params) {
  3. String? p1 = params[ParamsPage.k1]?.first;
  4. String? p2 = params[ParamsPage.k2]?.first;
  5. String? p3 = params[ParamsPage.k3]?.first;
  6. return ParamsPage(p1: p1, p2: p2, p3: p3);
  7. }));

7.3、复杂对象 方式

  1. fluroRouter.define(paramsObjectPage, handler: Handler(handlerFunc:
  2. (BuildContext? context, Map<String, List<String>> parameters) {
  3. var model = context?.settings?.arguments;
  4. if (model != null && model is InfoModel) {
  5. return ObjectPage(userInfo: model);
  6. }
  7. return ObjectPage();
  8. }));

8、配置使用

通常在main.dart文件中,

  1. class MyApp extends StatelessWidget {
  2. @override
  3. Widget build(BuildContext context) {
  4. // 定义路由
  5. final router = FluroRouter();
  6. // 配置路由(初始化)
  7. BaseRouter.registerConfigureRoutes(router);
  8. // 指定路由对象以便调用
  9. BaseRouter.setRouter(router);
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. primarySwatch: Colors.blue,
  14. ),
  15. home: HomePage(),
  16. // 设置路由
  17. onGenerateRoute: BaseRouter.getRouter()?.generator,
  18. );
  19. }
  20. }