go_router_sugar 1.0.0
go_router_sugar: ^1.0.0 copied to clipboard
Zero-effort file-based routing for Flutter with go_router. Automatically generates routing configuration from your file structure.
๐ฌ Go Router Sugar #
Zero-effort file-based routing for Flutter that automatically generates your go_router
configuration from your file structure. No more boilerplate, no more manual route definitions, and now with beautiful page transitions!
๐ฅ File-Based Routing Revolution #
Transform your Flutter app routing from complex configuration hell to intuitive file organization:
โ Before: Manual go_router Hell #
// 50+ lines of repetitive GoRoute configuration
GoRouter(
routes: [
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!; // Runtime error risk!
return ProductPage(id: id);
},
),
GoRoute(
path: '/user/profile/settings',
builder: (context, state) => const UserProfileSettingsPage(),
),
// ...endless boilerplate for every route
],
);
// String-based navigation (typo = crash!)
context.go('/prodcuts/123'); // Oops! Typo = runtime error
โ After: Effortless File-Based Magic #
# Just create files - routes are auto-generated!
lib/pages/
โโโ products/[id]_page.dart โ /products/:id
โโโ user/profile/settings_page.dart โ /user/profile/settings
โโโ home_page.dart โ /home
# Generate perfect router code with one command!
dart pub global activate go_router_sugar
go_router_sugar
// Type-safe navigation (autocomplete + compile-time safety!)
Navigate.goToProductsId(id: '123'); // โ
No typos possible!
Navigate.goToUserProfileSettings(); // โ
IntelliSense autocomplete!
// Use generated router instantly
MaterialApp.router(routerConfig: AppRouter.router);
๐ The Result: 90% Less Code, 100% More Safety #
- 5 minutes setup vs 2 hours of manual configuration
- Zero boilerplate - your file system IS your route map
- Type-safe navigation - impossible to make typo errors
- Beautiful transitions - professional animations with one line
- Hot reload friendly - changes reflect instantly
๐ฏ Usage Guide: Simple vs Advanced #
๐ SIMPLE (90% of developers - recommended start) #
Just want file-based routing? Start here!
// 1. Create pages in lib/pages/
lib/pages/home_page.dart
lib/pages/about_page.dart
lib/pages/products/[id]_page.dart
// 2. Generate routes
dart pub global activate go_router_sugar
go_router_sugar
// 3. Done! Type-safe navigation ready
Navigate.goToHome();
Navigate.goToProductsId(id: '123');
โก ADVANCED (10% of developers - enterprise features) #
Need guards, analytics, custom transitions? Add these:
@RouteGuards([AuthGuard()])
@PageTransition(TransitionConfig.slideRight)
@RouteAnalytics(trackPageViews: true)
class PremiumPage extends StatelessWidget { ... }
โจ Features #
- ๐ Zero Boilerplate: Automatically generates
GoRouter
configuration - ๐ File-Based Routing: Your file system becomes your route map
- ๐จ Rich Page Transitions: 15+ built-in transition types with zero effort
- ๐ง Highly Configurable: Customize directories, naming conventions, transitions, and output
- ๐ก๏ธ Type-Safe Navigation: Generated navigation helpers prevent runtime errors
- โก Dynamic Routes: Built-in support for path parameters using
[param]
syntax - ๐ฏ Flutter-First: Designed specifically for Flutter's ecosystem
- ๐ฑ Hot Reload Friendly: Works seamlessly with Flutter's development workflow
- ๐ญ Per-Page Transitions: Configure transitions individually for each page
- ๐ Route Guards: Built-in authentication and permission guards
- ๐๏ธ Shell Routes: Automatic layout detection for nested navigation
- ๐ Query Parameters: Type-safe query parameter handling
- ๐ Analytics: Built-in route analytics and performance monitoring
- ๐ ๏ธ CLI Tool: Command-line tool for scaffolding and page generation
- ๐ VS Code Extension: Rich IDE integration with IntelliSense and code actions
๐ Quick Start #
1. Installation #
Add to your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
go_router: ^16.1.0
dev_dependencies:
build_runner: ^2.4.9
go_router_sugar: ^1.0.0
2. Create Your Pages #
Create your page files in lib/pages/
(or your preferred directory):
lib/
โโโ pages/
โ โโโ home_page.dart
โ โโโ about_page.dart
โ โโโ profile/
โ โ โโโ settings_page.dart
โ โโโ products/
โ โโโ [id]_page.dart # Dynamic route: /products/:id
โ โโโ list_page.dart
โโโ main.dart
Basic page example (lib/pages/products/[id]_page.dart
):
import 'package:flutter/material.dart';
class ProductPage extends StatelessWidget {
final String id;
const ProductPage({super.key, required this.id});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Product $id')),
body: Center(
child: Text('Displaying product with ID: $id'),
),
);
}
}
Page with custom transition (lib/pages/animated_page.dart
):
import 'package:flutter/material.dart';
import 'package:go_router_sugar/go_router_sugar.dart';
@PageTransition(TransitionConfig.slideRight)
class AnimatedPage extends StatelessWidget {
const AnimatedPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Animated Page')),
body: const Center(
child: Text('This page slides in from the right!'),
),
);
}
}
3. Generate Routes #
๐ Easy way (recommended):
# Install globally once
dart pub global activate go_router_sugar
# Basic usage with default lib/pages directory
go_router_sugar
# Custom pages directory
go_router_sugar --pages-dir lib/screens
# Custom directory and output file
go_router_sugar -d lib/views -o lib/my_router.g.dart
# Watch for changes (auto-regenerates)
go_router_sugar_watch --watch --pages-dir lib/screens
โ๏ธ Traditional way:
dart run build_runner build
๐ก Pro tip: Use watch mode during development for instant updates!
4. Use the Generated Router #
import 'package:flutter/material.dart';
import 'app_router.g.dart'; // Generated file
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'My App',
routerConfig: AppRouter.router,
);
}
}
5. Navigate with Type Safety #
// Using generated navigation helpers
Navigate.goToHome();
Navigate.pushToProductsId(id: 'abc-123');
// Using GoRouter extensions
context.goToAbout();
context.pushToProfileSettings();
// Using route constants
context.go(Routes.home);
context.push(Routes.productsId.replaceAll(':id', 'abc-123'));
๐จ Page Transitions #
Go Router Sugar includes a comprehensive transition system with 10+ built-in animation types. Developers can choose from various transitions with zero configuration effort!
Available Transition Types #
Transition | Description | Use Case |
---|---|---|
platform |
Platform-specific default | Most pages (follows iOS/Android conventions) |
fade |
Smooth fade in/out | Modal dialogs, overlays |
slideRight |
Slide from right to left | Forward navigation (iOS style) |
slideLeft |
Slide from left to right | Back navigation, reverse flow |
slideUp |
Slide from bottom to top | Modal sheets, bottom navigation |
slideDown |
Slide from top to bottom | Notifications, dropdown menus |
scale |
Scale up from center | Pop-up effects, emphasis |
rotation |
Rotate while transitioning | Creative transitions, games |
size |
Size-based transition | Zoom effects, focus changes |
slideAndFade |
Combined slide + fade | Elegant page transitions |
none |
No animation | Instant navigation, performance |
Using Transitions #
Method 1: Page-Level Annotations (Recommended)
import 'package:flutter/material.dart';
import 'package:go_router_sugar/go_router_sugar.dart';
// Fade transition with custom duration
@PageTransition(TransitionConfig.fade)
class AboutPage extends StatelessWidget {
const AboutPage({super.key});
// ... widget implementation
}
// Slide transition with custom configuration
@PageTransition(TransitionConfig(
type: PageTransitionType.slideUp,
duration: Duration(milliseconds: 300),
curve: Curves.easeInOut,
))
class ModalPage extends StatelessWidget {
const ModalPage({super.key});
// ... widget implementation
}
// Quick preset configurations
@PageTransition(TransitionConfig.slideRight) // iOS-style push
@PageTransition(TransitionConfig.scale) // Pop-up effect
@PageTransition(TransitionConfig.none) // No animation
Method 2: Custom Transition Configs
// Create your own transition configurations
class MyTransitions {
static const fastFade = TransitionConfig(
type: PageTransitionType.fade,
duration: Duration(milliseconds: 150),
curve: Curves.easeOut,
);
static const bouncySlide = TransitionConfig(
type: PageTransitionType.slideRight,
duration: Duration(milliseconds: 500),
curve: Curves.bounceOut,
);
}
@PageTransition(MyTransitions.fastFade)
class QuickPage extends StatelessWidget {
// ... implementation
}
Transition Configuration Options #
class TransitionConfig {
final PageTransitionType type;
final Duration duration; // Default: 250ms
final Curve curve; // Default: Curves.easeInOut
const TransitionConfig({
required this.type,
this.duration = const Duration(milliseconds: 250),
this.curve = Curves.easeInOut,
});
}
๐ง Configuration #
CLI Options (Recommended) #
Generate with custom settings:
# Install globally once (one-time setup)
dart pub global activate go_router_sugar
# Custom pages directory
go_router_sugar --pages-dir lib/screens
# Custom output file
go_router_sugar --output lib/my_router.g.dart
# Both custom directory and output
go_router_sugar -d lib/views -o lib/app_routes.g.dart
# Watch mode with custom settings
go_router_sugar_watch --watch --pages-dir lib/screens
Common directory patterns:
lib/pages/
- Default (recommended)lib/screens/
- Alternative naminglib/views/
- MVC patternlib/ui/pages/
- Nested structure
Manual Configuration (build.yaml) #
Create a build.yaml
file in your project root to customize the generator:
targets:
$default:
builders:
go_router_sugar|routes:
options:
# Directory containing your page files
pages_directory: "lib/screens"
# Whether to generate type-safe navigation helpers
generate_navigation_helpers: true
# Name of the generated router class
router_class_name: "AppRouter"
# Output file path
output_file: "lib/my_router.g.dart"
๐ File Naming Conventions #
- Page files must end with
_page.dart
- Dynamic parameters use square brackets:
[id]_page.dart
,[category]_page.dart
- Nested routes follow your directory structure
Examples #
File Path | Generated Route | Widget Class |
---|---|---|
lib/pages/home_page.dart |
/home |
HomePage |
lib/pages/user/profile_page.dart |
/user/profile |
UserProfilePage |
lib/pages/products/[id]_page.dart |
/products/:id |
ProductPage |
lib/pages/blog/[year]/[month]_page.dart |
/blog/:year/:month |
BlogYearMonthPage |
๐ ๏ธ Generated Code #
The generator creates several helpful utilities:
1. Router Configuration #
class AppRouter {
static final GoRouter instance = GoRouter(routes: [...]);
static GoRouter get router => instance;
}
2. Route Constants #
abstract class Routes {
static const String home = '/home';
static const String productsId = '/products/:id';
// ... more routes
}
3. Type-Safe Navigation #
class Navigate {
static void goToHome() => AppRouter.instance.go('/home');
static void pushToProductsId({required String id}) =>
AppRouter.instance.push('/products/$id');
}
extension AppRouterNavigation on GoRouter {
void goToHome() => go('/home');
void pushToProductsId({required String id}) => push('/products/$id');
}
๐ฑ Advanced Usage #
Custom Page Classes #
Your page widgets can have any constructor signature. The generator will automatically pass path parameters:
class UserPage extends StatelessWidget {
final String userId;
final String? tab; // Optional parameters work too
const UserPage({
super.key,
required this.userId,
this.tab,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('User: $userId')),
body: tab != null
? Text('Tab: $tab')
: const Text('Default view'),
);
}
}
Nested Navigation #
Create complex nested routes by organizing your files:
lib/pages/
โโโ dashboard/
โ โโโ dashboard_page.dart # /dashboard
โ โโโ analytics/
โ โ โโโ analytics_page.dart # /dashboard/analytics
โ โโโ settings/
โ โโโ settings_page.dart # /dashboard/settings
Integration with GoRouter Features #
The generated router is fully compatible with GoRouter's features:
class AppRouter {
static final GoRouter instance = GoRouter(
routes: [...], // Generated routes
// Add your custom configuration
initialLocation: '/home',
errorBuilder: (context, state) => const ErrorPage(),
redirect: (context, state) {
// Add authentication logic
if (!isLoggedIn && state.location != '/login') {
return '/login';
}
return null;
},
);
}
๐งช Example Projects #
Minimal Example (Quick Start) #
Check out the minimal example to see basic file-based routing in action:
cd example/minimal
flutter pub get
dart pub global activate go_router_sugar
go_router_sugar
flutter run
What's included:
- Just 2 simple pages (
home_page.dart
,about_page.dart
) - Clean setup with zero advanced features
- Perfect for understanding the basics
Full Example (All Features) #
Explore the complete example with all advanced features:
cd example
flutter pub get
dart run build_runner build
flutter run
What's included:
- Multiple page types and transitions
- Advanced features (guards, analytics, custom directories)
- Complex routing scenarios
- Production-ready patterns
๐ Development Workflow #
Watch Mode #
For continuous generation during development:
go_router_sugar_watch --watch
Clean Build #
To regenerate all files:
dart run build_runner clean
dart run build_runner build
IDE Integration #
Most IDEs will automatically run the generator when you save files. You can also set up file watchers for seamless development.
๐ค Contributing #
We welcome contributions! Please see our Contributing Guide for details.
Development Setup #
- Clone the repository
- Run
flutter pub get
- Make your changes
- Run tests:
dart test
- Submit a pull request
๐ License #
This project is licensed under the MIT License - see the LICENSE file for details.
๐โโ๏ธ Support #
- ๐ Documentation
- ๐ Issue Tracker
- ๐ฌ Discussions
Made with โค๏ธ for the Flutter community