csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 97 forks source link

Allow sub route path to be absolute path #39

Closed jezsung closed 3 years ago

jezsung commented 3 years ago

Let's say I want the route paths to be like '/login and /sign-up. And I want them to be stacked so that if users first come in /login and go to /sign-up and then try to go back to /login again, it will allow them to easily go back by pressing the back button on Android or swipe right on iOS.

But the current implementation seems forces me to set sub route paths to not start with /. It forces me the /sign-up path to be /login/sign-up.

csells commented 3 years ago

Hey, @jezsung, thanks for the feedback. Personally, I like my login and signup page to be a single page w/ panels/tabs. However, even if you want them to be separate pages, you can get the behavior you want like so:

GoRouter(routes: [
    GoRoute(path: '/', builder: ...HomePage...),
    GoRoute(path: '/login', builder: ...LoginPage...),
    GoRoute(path: '/signup', builder: ...SignupPage...),
  ],

  error: ...

  redirect: (state) {
    final loggedIn = ...
    final loggingIn = state.subloc == '/login' || state.subloc == '/signup';

    if( !loggedIn && !loggingIn ) return '/login';
    if( loggedIn && loggingIn ) return '/';
    return null;
  },
}

If the user isn't logged in, they'll be sent to /login. If they press Sign Up instead and you send them to '/signup' via context.go('/signup'), that's the link that will show in the address bar. They won't have a Back button on the AppBar but if they press Back on the browser, they'll be redirected to '/login' again, which I think is what you're after.

Does that allow you to do what you want?

jezsung commented 3 years ago

@csells This would be ideal for the web, but I want the app bar to have the back button on mobile. This could be achieved by the push function that you mentioned on the other issue. Will that feature be implemented?

csells commented 3 years ago

@jezsung I think your sign-up page would have a button to login instead, right? I don't think you'd want a Back button on the App bar in this case.

However, yes, I do plan to implement this feature.

csells commented 3 years ago

dup: https://github.com/csells/go_router/issues/9