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

add a code sample to include app init/splash screen #131

Closed csells closed 2 years ago

csells commented 3 years ago

e.g.

// unnamed routes
redirect: (state) {
  // capture deep link
  final from = state.queryParams['from'] ?? state.subloc;

  // check for the app to be ready
  final initialized = appState.initialized;
  final initializing = state.subloc == '/init';
  if (!initialized && !initializing) return '/init?from=$from';

  // check for the user to be logged in
  final loggedIn = loginInfo.loggedIn;
  final loggingIn = state.subloc == '/login';
  if (!loggedIn && !loggingIn) return '/login?from=$from';
  if (loggedIn && loggingIn) return '/';

  // strip the deep link
  if( state.queryParams.contains('from') ) return state.subloc;

  // no redirect
  return null;
}

// named routes
redirect: (state) {
  // capture deep link
  final from = state.queryParams['from'] ?? state.subloc;

  // check for the app to be ready
  final initialized = appState.initialized;
  final initializing = state.subloc == state.namedLocation('init');

  if (!initialized && !initializing) {
    return state.namedLocation('init', queryParams: {'from': from);
  }

  // check for the user to be logged in
  final loggedIn = loginInfo.loggedIn;
  final loggingIn = state.subloc == state.namedLocation('login');

  if (!loggedIn && !loggingIn) {
    return state.namedLocation('login', queryParams: {'from': from});
  }

  if (loggedIn && loggingIn) return state.namedLocation('home');

  // strip the deep link
  if( state.queryParams.contains('from') ) return state.subloc;

  // no redirect
  return null;
}
csells commented 2 years ago

fixed in the loading page example: https://github.com/csells/go_router/blob/main/go_router/example/lib/loading_page.dart#L81