route_map 0.0.17 copy "route_map: ^0.0.17" to clipboard
route_map: ^0.0.17 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.

MIT License stars pub version Buy Me A Coffee

README Translation #

Table of Contents #

Tutorial #

Bottom navigation bar

web #

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 #

  1. First, define Route Generator with RouteMapInit annotation.
import 'route_map.config.dart';

        @RouteMapInit()
        Route? onGenerateRoute(RouteSettings routeSettings) => $onGenerateRoute(routeSettings);
  1. 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;
        });
  1. Define the route builder in the MaterialApp widget
MaterialApp(
        initialRoute: RouteMaps.splash, // defining the initial page
        onGenerateRoute: onGenerateRoute
);
  1. 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 {}
  1. 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);
  1. 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"});
  1. 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"];


  1. @RouteMapArg() attribute should be used for custom model.
@RouteMapArg()
class CustomModel {
  String name;
  CustomModel({required this.name});
}
  1. 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  

Support the Library #

  • You can support the library by staring it on Github && liking it on pub.dev or report any bugs you encounter.
  • Also, if you have a suggestion or think something can be implemented in a better way, open an issue and let's talk about it.
16
likes
160
pub points
63%
popularity

Publisher

verified publisheremintolgahanpolat.com

Route Map is a convenient code generator for Navigator 2.0. Inspired by injectable.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

flutter

More

Packages that depend on route_map