A Simple Flutter Page Jump Router
dependencies:
easy_router: 0.9.4 #latest version
//True means that the parameter is required, and the constructor that passes the
//EasyRouteParam parameter must be added.
@EasyRoutePathAnnotation("pageA", true)
class PageA extends StatelessWidget {
final EasyRouteParam param;
PageA(this.param);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page A"),
),
body: Container(
alignment: Alignment.center,
child: Text("param:${param["key"]}"),
),
);
}
}
//False means no parameters are required, no need to add a constructor that passes the
//EasyRouteParam parameter
@EasyRoutePathAnnotation("pageB", false)
class PageB extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Page B"),
),
body: Container(
alignment: Alignment.center,
child: Text("no param"),
),
);
}
}
Use @EasyRouterAnnotation() to annotate the custom class before you can generate the relevant code with the command, for example
@EasyRouterAnnotation()
class Router {
}
cd to your app module and execute the command
flutter packages pub run build_runner build --delete-conflicting-outputs
It is recommended to clean up the previous code before building
flutter packages pub run build_runner clean
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return EasyRouter.instance.getWidget("pageA", {"key": "a"});
//or
//return EasyRouter.instance.getWidget("pageB");
},
),
);
}
Recommend wrapping EasyRouter
, refer to router.dart
in demo
To learn how to generate code using source_gen, check out My Blog
Plugins | Status |
---|---|
source_gen | |
build_config | |
mustache4dart |
the license is MIT