page_route_generator 0.0.1
page_route_generator: ^0.0.1 copied to clipboard
Page Route Generator is a build-time utility for Flutter that helps you automatically generate and manage routes for your app.
Page Route Generator #
Page Route Generator is a build-time utility for Flutter that helps you automatically generate and manage routes for your app. With this package, you can easily fetch route names for your widgets using a type-safe approach.
Features #
- Automatic Route Management: Generates route names for all annotated pages (
@page) in your app. - Type-Safe Route Access: Access routes using widget types with the
R<T>helper. - Error Handling: Provides clear error messages when a route is not found.
- Centralized Route List: All routes are stored in a single file for easy management.
Installation #
-
Add the package to your
pubspec.yamlas a dependency:dependencies: page_route_generator: ^latest -
Add
build_runneras a dev dependency:dev_dependencies: build_runner: ^2.4.0 -
Run
flutter pub getto install dependencies.
Usage #
1. Annotate Your Widgets #
Add the @page annotation to any widget that should have a route:
import 'package:page_route_generator/page_annotation.dart';
import 'package:flutter/material.dart';
@page
class Master extends StatelessWidget {
@override
Widget build(BuildContext context) => Container();
}
2. Generate Routes #
Run the following command to generate the route list:
flutter pub run build_runner build --delete-conflicting-outputs
This will create a generated_route.dart file in your lib folder.
3. Use Routes in MaterialApp #
You can integrate the generated routes directly into MaterialApp using the routes property:
import 'generated_route.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: R<Master>.route, // Use the generated route name
routes: {
for (final route in generatedRoutes) route.routeName: (context) => route.widget,
},
);
}
}
Error Handling #
If you try to access a route for a widget that doesn't exist, the system will throw an exception:
print(R<NonExistentWidget>.route);
// Exception: Route not found for widget type: NonExistentWidget
Example Generated File #
Here's an example of what the generated generated_route.dart file looks like:
// GENERATED CODE - DO NOT MODIFY BY HAND
// This file will be regenerated on each build.
import 'package:example/features/pages/master/master.dart';
import 'package:flutter/widgets.dart';
class RouteType {
final String routeName;
final Type type;
final Widget widget;
RouteType(this.routeName, this.type, this.widget);
}
final List<RouteType> generatedRoutes = [
RouteType('master', Master, Master()),
];
class R<T> {
String get route {
try {
return generatedRoutes.firstWhere((element) => element.type == T).routeName;
} catch (e) {
throw Exception("Route not found for widget type: \$T");
}
}
}
Contributing #
Contributions are welcome! If you encounter any issues or have ideas for improvements, feel free to open an issue or submit a pull request.
License #
This project is licensed under the MIT License. See the LICENSE file for details.