go_router_paths 0.2.0 copy "go_router_paths: ^0.2.0" to clipboard
go_router_paths: ^0.2.0 copied to clipboard

Helps define paths for `go_router` and other navigation packages. Remove those hard coded strings!

example/main.dart

import 'package:go_router_paths/go_router_paths.dart';

// Define the paths for your app
class AppPaths {
  static Path get home => Path('home');
  static WelcomePath get welcome => WelcomePath();

  static UsersPath get users => UsersPath();
  static Param<Param> get books => Param('books', 'bookId');
}

class WelcomePath extends Path<WelcomePath> {
  WelcomePath() : super('welcome');

  Path get login => Path('login', parent: this);
  Path get register => Path('register', parent: this);
}

class UsersPath extends Path<UsersPath> {
  UsersPath() : super('users');

  UserPath get user => UserPath(this);
}

class UserPath extends Param<UserPath> {
  UserPath(UsersPath usersPath) : super.only('userId', parent: usersPath);

  Path get edit => Path('edit', parent: this);
  Path get delete => Path('delete', parent: this);
}

// Define the routes for your app
final routes = GoRouter(
  routes: [
    GoRoute(
      path: AppPaths.home.goRoute,
      pageBuilder: (context, state) => const HomePage(),
    ),
    GoRoute(
      path: AppPaths.welcome.goRoute,
      pageBuilder: (context, state) => const WelcomePage(),
      routes: [
        GoRoute(
          path: AppPaths.welcome.login.goRoute,
          pageBuilder: (context, state) => const LoginPage(),
        ),
        GoRoute(
          path: AppPaths.welcome.register.goRoute,
          pageBuilder: (context, state) => const RegisterPage(),
        ),
      ],
    ),
    GoRoute(
      path: AppPaths.users.goRoute,
      pageBuilder: (context, state) => const UsersPage(),
      routes: [
        GoRoute(
          path: AppPaths.users.user.goRoute,
          pageBuilder: (context, state) {
            final userId = state.params[AppPaths.users.user.id]!;

            return UserPage(userId);
          },
          routes: [
            GoRoute(
              path: AppPaths.users.user.edit.goRoute,
              pageBuilder: (context, state) {
                final userId = state.params[AppPaths.users.user.id]!;

                return EditUserPage(userId);
              },
            ),
            GoRoute(
              path: AppPaths.users.user.delete.goRoute,
              pageBuilder: (context, state) {
                final userId = state.params[AppPaths.users.user.id]!;

                return DeleteUserPage(userId);
              },
            ),
          ],
        ),
      ],
    ),
    GoRoute(
      path: AppPaths.books.goRoute,
      pageBuilder: (context, state) {
        final bookId = state.params[AppPaths.books.id]!;

        return BookPage(bookId);
      },
    ),
  ],
);

// Use the routes in your app
void main() {
  AppPaths.home.path; // '/home'
  AppPaths.welcome.login.path; // '/welcome/login'
  AppPaths.welcome.register.path; // '/welcome/register'
  AppPaths.users.path; // '/users'
  AppPaths.users.user.define('Luke Skywalker').path; // '/users/Luke Skywalker'
  AppPaths.users.user
      .define('Jar Jar Banks')
      .edit
      .path; // '/users/Jar Jar Banks/edit'
  AppPaths.users.user
      .define('Darth Vader')
      .delete
      .path; // '/users/Darth Vader/delete'
  AppPaths.books.define('Star Wars').query({
    'first-trilogy-only': 'true'
  }).path; // '/books/Star Wars?first-trilogy-only=true'
}
11
likes
0
pub points
85%
popularity

Publisher

verified publishermrgnhnt.com

Helps define paths for `go_router` and other navigation packages. Remove those hard coded strings!

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on go_router_paths