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

outdated

A new Flutter package.

An un-opinionated url-based Router controller for Navigator 2.

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
  • 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: ^1.0.4

🕹ī¸ 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