go_router_auto 0.0.1
A package that simplifies route management with go_router using code generation.
go_router_auto
A Flutter package that simplifies route management with go_router using code generation. Say goodbye to manual route configuration and hello to auto-generated routes!
Features
- 🚀 Automatically generate route configurations
- 🧩 Seamless integration with go_router
- 🎯 Type-safe route definitions
- 🔧 Easy to use with minimal setup
Getting Started
1. Install the package
Add go_router_auto to your pubspec.yaml:
dependencies:
go_router_auto: ^0.0.1
dev_dependencies:
build_runner: ^2.4.0
2. Configure build.yaml
Create or update your build.yaml file in the root of your project:
targets:
$default:
builders:
go_router_auto|route_generator:
enabled: true
3. Use the @AutoRoute annotation
In your page files, use the @AutoRoute() annotation on a static route field:
import 'package:flutter/material.dart';
import 'package:go_router_auto/go_router_auto.dart';
class HomePage extends StatelessWidget {
@AutoRoute()
static final route = _HomePageRoute();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: Center(child: Text('Welcome to go_router_auto!')),
);
}
}
class _HomePageRoute {
String path() => '/';
GoRoute createRoute() {
return GoRoute(
path: path(),
builder: (context, state) => HomePage(),
);
}
}
4. Generate routes
Run the following command to generate your routes:
flutter pub run build_runner build
5. Use generated routes
Create an app_router.dart file:
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
part 'app_router.routes.dart';
class AppRouter {
static final GoRouter router = GoRouter(
routes: [
..._$appRoutes,
],
);
}
Then use it in your main.dart:
import 'package:flutter/material.dart';
import 'app_router.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: AppRouter.router,
title: 'go_router_auto Demo',
);
}
}
Advanced Usage
Nested Routes
You can easily create nested routes by defining child routes in your _Route class:
class _ParentPageRoute {
String path() => '/parent';
GoRoute createRoute() {
return GoRoute(
path: path(),
builder: (context, state) => ParentPage(),
routes: [
_ChildPageRoute().createRoute(),
],
);
}
}
class _ChildPageRoute {
String path() => 'child';
GoRoute createRoute() {
return GoRoute(
path: path(),
builder: (context, state) => ChildPage(),
);
}
}
Route Parameters
You can include route parameters in your path:
class _UserPageRoute {
String path() => '/user/:id';
GoRoute createRoute() {
return GoRoute(
path: path(),
builder: (context, state) => UserPage(id: state.pathParameters['id']!),
);
}
}
Contributing
Contributions are welcome! If you find a bug or want a feature, please open an issue. If you want to contribute code, please fork the repository and submit a pull request.
License
This project is licensed under the MIT License - see the LICENSE file for details.