route_map 0.0.14 route_map: ^0.0.14 copied to clipboard
Route Map is a convenient code generator for Navigator 2.0. Inspired by injectable.
Route Map #
Generate route paths map for Flutter Navigator 2 using annotation.
README Translation #
Table of Contents #
Tutorial #
#
Installation #
dependencies:
# add route_map to your dependencies
route_map:
dev_dependencies:
# add the generator to your dev_dependencies
route_map_generator:
# add build runner if not already added
build_runner:
Setup #
- First, define Route Generator with RouteMapInit annotation.
import 'route_map.config.dart';
@RouteMapInit()
Route? onGenerateRoute(RouteSettings routeSettings) => $onGenerateRoute(routeSettings);
- Redirection changes the location to a new one based on application state. For example, redirection can be used to display a sign-in screen if the user is not logged in.
import 'route_map.config.dart';
@RouteMapInit()
Route? onGenerateRoute(RouteSettings routeSettings) => $onGenerateRoute(routeSettings,redirect:(path){
bool isLogin = false;
if(isLogin && RouteMaps.homePage == path){
return RouteMap.login;
}
return null;
});
- Define the route builder in the
MaterialApp
widget
MaterialApp(
initialRoute: RouteMaps.splash, // defining the initial page
onGenerateRoute: onGenerateRoute
);
- Annotate your pages with
@RouteMap
and let the generator do the work.
Note: Use "/" to specify the root directory. To create a root-independent page, there must be no "/" at the beginning.
@RouteMap(name: "splash")
class SplashPage extends StatefulWidget {}
@RouteMap(name: "/")
class HomePage extends StatefulWidget {}
@RouteMap(name: "/search", fullScreenDialog: true)
class SearchPage extends StatefulWidget {}
@RouteMap(name: "/detail", path:"/detail/:id/:name")
class DetailPage extends StatefulWidget {}
- You can take advantage of the class object to be redirected when passing data between pages. You can use all the functions of the standart Navigator class.
DetailPageRoute(id: "0",name: "push").push(context);
DetailPageRoute(id: "0",name: "push").popAndPush(context);
- Redirection between pages using standart Navigator class
The arguments field can be used to send values during routing.
Navigator.of(context)
.pushNamed(RouteMaps.detailPage, arguments: "value");
Navigator.of(context)
.pushNamed(RouteMaps.detailPage, arguments: {"val1":"Easy","val2":"Route"});
- Reading the values with
routeArgs()
passed from previous screen via Navigator.
String value = context.routeArgs();
String val1 = context.routeArgs()["val1"];
String val2 = context.routeArgs()["val2"];
- @RouteMapArg() attribute should be used for custom model.
@RouteMapArg()
class CustomModel {
String name;
CustomModel({required this.name});
}
- Customized builder is available. For example, you might want to wrap your page with ChangeNotifierProvider.
Widget homeBuilder(Widget child) {
return ChangeNotifierProvider(
create: (_) => HomeViewModel(),
child: child,
);
}
@RouteMap(name: "home", builder: homeBuilder)
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
I need help. #
URL-based page redirection is still experimental. There are issues regarding type conversions, and it hasn't been thoroughly tested yet.
I need assistance with type conversions in URL-based page redirection. It supports int, double, string, and bool.
Run the generator #
Use the [watch] flag to watch the files' system for edits and rebuild as necessary.
flutter packages pub run build_runner watch --delete-conflicting-outputs
if you want the generator to run one time and exits use
flutter packages pub run build_runner build --delete-conflicting-outputs
Problems with the generation? #
Make sure you always Save your files before running the generator, if that does not work you can always try to clean and rebuild.
flutter packages pub run build_runner clean