route_map 0.0.6 route_map: ^0.0.6 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 #
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);
- 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 {}
- 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.
DetailPage(id: "0",name: "push").push(context);
DetailPage(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"];
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