better_router 2.1.0 icon indicating copy to clipboard operation
better_router: ^2.1.0 copied to clipboard

the better router is a powerful, but for humans, flutter router.

A powerful, but for humans, flutter router

Features #

  • Static routes
  • Dynamic routes
  • Catch All

Getting started #

$ flutter pub add better_router

Usage #

void main() {
  final books = [
    const Book(id: '3160', name: 'The Odyssey', author: 'Homer'),
    const Book(
        id: '8795', name: 'The Divine Comedy', author: 'Dante Alighieri'),
  ];

  final betterRoutes = BetterRouter(routes: {
    '/': DefaultPageRouteBuilder((context) => Column(children: [
          Text('root page'),
          TextButton(
              onPressed: () => Navigator.pushNamed(context, "/books"),
              child: Text("Books")),
           TextButton(
            onPressed: () =>
                Navigator.pushNamed(context, "/this_page_does_not_exists"),
            child: Text("Go to not found page")),
          TextButton(
            onPressed: () =>
                Navigator.pushNamed(context, "/custom_route_transition"),
            child: Text("Go to custom transition"))
        ])),
    '/home': DefaultPageRouteBuilder((_) => Text('home page')),
    '/books': DefaultPageRouteBuilder((context) => Column(children: [
          for (var i = 0; i < books.length; i++)
            TextButton(
                onPressed: () =>
                    Navigator.pushNamed(context, "/books/${books[i].id}"),
                child: Text(books[i].name))
        ])),
    r"\/books\/(?<id>.+)": DefaultPageRouteBuilder((context) {
      final params = RouteParams.of(context);

      return Column(
        children: [Text('book page'), Text("Book ID: ${params['id']}")],
      );
    }),
    '/custom_route_transition': (RouteSettings settings) => PageRouteBuilder(
      pageBuilder: (context, animation, secondaryAnimation) =>
          Text("Transition \o/"),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        var begin = Offset(0.0, 1.0);
        var end = Offset.zero;
        var tween = Tween(begin: begin, end: end);
        var offsetAnimation = animation.drive(tween);

        return SlideTransition(
          position: offsetAnimation,
          child: child,
        );
    }),
    '*matchAll': PageRouteBuilder((_) => Text('not found page')),
  });

  runApp(MaterialApp(onGenerateRoute: betterRoutes));
}
1
likes
130
pub points
30%
popularity

Publisher

verified publisher iconemersonalmeida.wtf

the better router is a powerful, but for humans, flutter router.

Repository (GitHub)

Documentation

API reference

License

Icon for licenses.BSD-3-Clause (LICENSE)

Dependencies

flutter

More

Packages that depend on better_router