a router config mapping solution based on source_gen through annotation
-
Notice: your page should own a constuctor that can be initialized with unique param, then Annotate the page class with @ARoute
example:@ARoute(url: 'myapp://pagea') class A { A(MyRouteOption option): super(); }
-
Annotate your own router class with @ARouteRoot
example:@ARouteRoot() class MyRouter {}
-
run the build_annotation_route.sh in your workspace Or just run the command below in your workspace
build:flutter packages pub run build_runner build --delete-conflicting-outputs
suggest you running the clean command before build:
clean:flutter packages pub run build_runner clean
-
now you can get the instance
example:@ARouteRoot() class Router { ARouterInternal internal = ARouterInternalImpl(); dynamic getPage(MyRouteOption option) { return internal.findPage( ARouteOption(option.urlpattern, option.params), option); } } class MyRouteOption { String urlpattern; Map<String, dynamic> params; }
-
examples placed in 'lib/example/*'
add packages to dev_dependencies in your pubspec.yaml
example:
dev_dependencies:
annotation_route: any
clone the code, then put it into your workspace, announce it in your pubspec.yaml exxample:
dev_dependencies:
annotation_route:
path: annotation_route
一个以注解方式实现的路由映射解决方案,基于 source_gen
-
注意: 你的页面应该实现指定的构造器,一个接受唯一参数的构造器,然后使用@ARoute 注解你的页面类
例:@ARoute(url: 'myapp://pagea') class A { A(MyRouteOption option): super(); }
-
使用 @ARouteRoot 注解 你自己的 router 类 例:
@ARouteRoot() class MyRouter {}
-
在你的工作目录下运行 build_annotation_route.sh 脚本,或者运行如下命令:
build:flutter packages pub run build_runner build --delete-conflicting-outputs
建议在执行 build 命令前,先执行如下命令:
clean:flutter packages pub run build_runner clean
-
现在你可以使用它来获取类的实例了
例:@ARouteRoot() class Router { ARouterInternal internal = ARouterInternalImpl(); dynamic getPage(MyRouteOption option) { return internal.findPage( ARouteOption(option.urlpattern, option.params), option); } } class MyRouteOption { String urlpattern; Map<String, dynamic> params; }
-
具体代码可以参考 lib/example/*下的文件
在你的 pubspec.yaml 文件下声明
例:
dev_dependencies:
annotation_route: any
拷贝代码至你的工作目录,在你的 pubspec.yaml 文件下声明
例:
dev_dependencies:
annotation_route:
path: annotation_route