url_router 0.1.0+1 copy "url_router: ^0.1.0+1" to clipboard
url_router: ^0.1.0+1 copied to clipboard

outdated

An un-opinionated url-based Router implementation (Navigator 2.0)

An un-opinionated url-based Router implementation (Navigator 2.0).

late final router = UrlRouter(
  onGeneratePages: (router) => [
    MaterialPage(child: MainView(router.url)
]);

@override
Widget build(BuildContext context) {
    return MaterialApp.router(
      routeInformationParser: UrlRouter.parser,
      routerDelegate: router,
    );
}

UrlRouter makes no assumptions about your UI layout. It simply calls onGeneratePages and expects you to return a stack of Page elements. How you react to router.url is up to you.

Features #

  • Easily read and update the current url and query params
  • Supports deep linking, protected urls, and redirects
  • Back and forward navigation in browser
  • Full control over url to page mapping, wildcards etc

🔨 Installation #

dependencies:
  url_router: ^0.1.0

🕹ī¸ Usage #

  • Declare a UrlRouter and implement the onGeneratePages method.
  • Return a list of Page elements that represent your desired navigator stack
  • Implement the optional onChanging and scaffoldBuilder delegates
late final router = UrlRouter(
  onChanging: (router, newUrl) {
    if (authorized == false) return '/';
    return newUrl;
  },
  scaffoldBuilder: (router, navigator) {
    return Row(
      children: [ const SideBar(), Expanded(child: navigator) ],
    );
  },
  onGeneratePages: (router) {
    return [
      // Main view is always present
      MaterialPage(child: MainView()),
      // Settings can sit on top of main view (and can be popped)
      if(router.url == '/settings')... [
         MaterialPage(child: SettingsView()),
      ]
    ];
  },
);

Controlling the url #

UrlRouter offers a small but powerful API to control the url:

API Description
.url read / update the current path
.push add a segment to the current path
.pop remove a segment from the current path
.onChanging called before path is changed, allows for protected paths and redirects
.queryParams access the current query parameters

Accessing the router #

Access the UrlRouter anywhere in your app, using UrlRouter.of(context), or use the context extensions:

  • context.url
  • context.urlPush
  • context.urlPop
  • context.urlRouter

Outer Scaffolding #

You can use the scaffoldBuilder delegate to wrap persistent UI around the underlying Navigator widget:

final router = UrlRouter(
    scaffoldBuilder: (router, navigator) {
      return Row(
        children: [
          SideBar(),
          Expanded(child: navigator),
        ],
      );
    },

🐞 Bugs/Requests #

If you encounter any problems please open an issue. If you feel the library is missing a feature, please raise a ticket on Github and we'll look into it. Pull request are welcome.

📃 License #

MIT License

21
likes
0
pub points
73%
popularity

Publisher

verified publishergskinner.com

An un-opinionated url-based Router implementation (Navigator 2.0)

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on url_router