hwh97 d3affe446c fix: 修复part of导入包识别问题 | 3 anni fa | |
---|---|---|
lib | 3 anni fa | |
.gitignore | 3 anni fa | |
LICENSE | 3 anni fa | |
README.md | 3 anni fa | |
build.yaml | 3 anni fa | |
pubspec.lock | 3 anni fa | |
pubspec.yaml | 3 anni fa |
router_gen 是一个通过注解实现自动化生成路由代码的工具
自动参数导入 路径自动生成 路由注册表生成
dependencies添加
router_gen:
git:
url: https://git.i2erp.cn/andy.huang/router_gen.git
dev_dependencies添加
build_runner: any
使用注解@routerTable或@RouterTable()定义路由表类,接着注册RouterTableProvider路由
import 'package:router_gen/router_gen.dart';
import 'package:school_parent/router_gen/router_table.route.dart';
import 'package:school_parent/base_plugin/routes.dart';
@routerTable
abstract class RouterTableProvider extends IRoutesProvider {
factory RouterTableProvider() => $RouterTableProvider();
}
注意这里import的router_table.route.dart是自动生成后的产物,与RouterTable在同一个文件夹
\@RouterPage(String path)
\@routerParam 或者 \@RouterParam()
上个例子 定义类如下
@RouterPage(path: "example")
class ExamplePage extends StatefulWidget {
@routerParam
final String param;
const ExamplePage({Key key, this.param}) : super(key: key);
@override
_ExamplePageState createState() => _ExamplePageState();
}
......
调用build_runner生成代码
flutter packages pub run build_runner build
生成路由表如下
// GENERATED CODE - DO NOT MODIFY BY HAND
// **************************************************************************
// RouterTableGenerator
// **************************************************************************
import 'router_table.dart';
import 'package:fluro/fluro.dart' as fluro;
import 'package:school_parent/base_plugin/routes.dart';
import 'package:school_parent/pages/example_page.dart';
class $RouterTableProvider implements RouterTableProvider {
static final example = "/pages/example";
@override
void registerRoutes(fluro.Router router) {
router.define(
example,
handler: fluro.Handler(
handlerFunc: (c, p) => ExamplePage(
param: IRoutesProvider.getParams<String>(p, "param"),
),
),
);
}
}