aym_router_generator 1.2.2+1 aym_router_generator: ^1.2.2+1 copied to clipboard
AYMRouter generator Dart libraries. A flutter routing generator that automatically generates routing tables when compiling. Need to use with aym_router.
AYMRouter generator Dart libraries. A flutter routing generator that automatically generates routing tables when compiling. Need to use with aym_router.
简介 #
一个以注解方式实现的路由映射解决方案,基于 bean_factory 与 bean_factory_generator
使用 #
-
你的页面包含任意多构造器(当然仅包含无参的默认也是可以的),会根据传入的参数自动寻找合适的构造,然后使用@RoutePage 注解你的页面类
例:@RoutePage("router://example.router.aymtools.com/test1") class A { String name; bool flag; int age; double height; //必须包含name其他非必须 A(this.name,{this.flag,this.age,this.height}); //必须包含height 无需其他参数 A.height(this.height); }
更多用法请参考example内容
-
在任意dart Source目录下创建beanfactory.dart并 @AYMRouterRoot 注解 任意的的 Class 例:
///寄存文件必须为beanfactory.dart @AYMRouterRoot() class RouterFactory {}
-
在你的工作目录下运行如下命令:
build:flutter packages pub run build_runner build --delete-conflicting-outputs
建议在执行 build 命令前,先执行如下命令:
clean:flutter packages pub run build_runner clean
-
接下来修改main.dart中的App中的onGenerateRoute
例:void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', initialRoute: "router://example.router.aymtools.com/test1", theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter Demo Home Page'), //修改此处,将路由的表交给AYMRouter处理 当然您也可以自己处理 onGenerateRoute: AYMRouter.instance.onGenerateRoute, ); } }
-
使用的具体方式依然同默认的Navigator
@RoutePage("router://example.router.aymtools.com/test2")
class Test2Page extends StatelessWidget{
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("第二个测试界面"),
),
body: Center(
child: GestureDetector(
onTap: () {
Navigator.of(context).pushNamed(
'router://example.router.aymtools.com/test1',
arguments: "this is title");
//这是调用height参数
Navigator.of(context).pushNamed(
"router://example.router.aymtools.com/test1.height",
arguments: 185);
//直接使用uri参数
Navigator.of(context).pushNamed(
'router://example.router.aymtools.com/test1?title=mytitle');
//使用map传入更多参数
Navigator.of(context).pushNamed(
'router://example.router.aymtools.com/test1',
arguments: {'title':'this is title','flag':true});
},
child: Text(
'更多内容',
),
)),
);
}
}
更多内容可以参考 example/*下的文件
安装 #
从 pub 安装 #
在你的 pubspec.yaml 文件下声明
例:
dependencies:
aym_router: any
dev_dependencies:
aym_router_generator: any