Mad Navigation CLI
A command-line utility for automatic generation and registration of navigation routes
in Flutter projects using the mad_scripts_base and mad_navigation.
It automates:
- ποΈ Creating new route classes (
Page,Dialog,BottomSheet,TabHolder); - π§© Updating the route mapper (
MadRouteMapper); - π§ Extending navigation services with new methods.
π Quick Start (CLI)
Add the package as a dependency to your scripts project:
dart pub add mad_navigation_cli --dev
Then run the built-in command via the mad scripts CLI:
mad add_route --page --name Settings --uiComponent SettingsPage()
Supported route types (choose one flag):
| Flag | Description |
|---|---|
--page |
Standard full-screen page |
--dialog |
Dialog that may return a result |
--bottomSheet |
Bottom sheet component |
--tabHolder |
Container route used inside tab navigation |
Example:
mad add_route --dialog --name ConfirmSignOut --uiComponent ConfirmSignOutDialog()
This will:
- Insert a new
DialogConfirmSignOutclass into your routes file; - Add an abstract method + implementation to navigation services;
- Register a new builder entry in the route mapper.
π§ Advanced Usage in Custom Scripts
You can reuse AddRoute, SimpleAddRoute, or even subclass them
inside your own mad_scripts_base commands.
Example 1: Full automation with SimpleAddRoute
final AddRouteConfig config =
ConfigReader.fromFile('mad_navigation.json', transformer: AddRouteConfig.fromJson);
const RouteMeta meta = RouteMetas.page;
final SimpleAddRoute add = SimpleAddRoute(
routeName: 'Settings',
config: config,
meta: meta,
);
await add.run(uiComponent: 'SettingsPage()');
Example 2: Extend AddRoute for custom behavior
class CustomAddRoute extends AddRoute {
const CustomAddRoute({
required super.routeName,
required super.config,
required super.meta,
});
@override
Future<void> addToService({
required String renderedServiceTemplate,
required String renderedServiceImplTemplate,
}) async {
print('[CustomAddRoute] Adding service methods for $routeName ...');
await super.addToService(
renderedServiceTemplate: renderedServiceTemplate,
renderedServiceImplTemplate: renderedServiceImplTemplate,
);
}
}
Example 3: Partial usage
Call only what you need β for instance, update just the mapper:
final AddRoute add = AddRoute(routeName: 'SelectTheme', config: config, meta: RouteMetas.bottomSheet);
await add.addMapper(
fullNewMapperTemplate: RouteTemplates.newMapper(
meta: RouteMetas.bottomSheet,
routeName: 'SelectTheme',
uiComponent: 'SelectThemeSheet()',
),
renderedTemplate: RouteTemplates.mapper(
meta: RouteMetas.bottomSheet,
routeName: 'SelectTheme',
uiComponent: 'SelectThemeSheet()',
),
);
For more complex examples see example.dart.
βοΈ Configuration
Routes, mappers, and service file paths are defined in your projectβs
mad_navigation.json (parsed into AddRouteConfig):
{
"routesPath": "lib/navigation/app_routes.dart",
"routeMapperPath": "lib/navigation/app_mapper.dart",
"servicePath": "lib/navigation/navigation_service.dart",
"serviceImplPath": "lib/navigation/navigation_service_impl.dart",
"addToService": true
}
π¨ Error Handling
All runtime errors during file parsing or modification throw typed exceptions:
| Exception | Meaning |
|---|---|
RouteInsertionException |
No matching base class found to insert route |
MapperNotFoundException |
Mapper class (e.g. MadRouteMapper) not found |
RoutersMethodInvalidException |
routers() method has invalid or unexpected body |
ServiceInsertionException |
Service or implementation class not found |
You can catch them easily:
try {
await add.run();
} on AddRouteException catch (e) {
print('Failed: ${e.runtimeType} β ${e.message}');
}
π§± Architecture Overview
| Component | Responsibility |
|---|---|
AddRoute |
Core low-level logic β AST parsing, code insertion |
SimpleAddRoute |
High-level orchestrator combining templates & logic |
RouteMeta / RouteMetas |
Describes route types (Page, Dialog, etc.) |
RouteTemplates |
String templates for classes, mappers, and services |
AddRouteCommand |
CLI command wrapper used by mad_scripts |
π¬ Feedback
This package is part of the Mad Scripts ecosystem.
If you encounter edge cases or need extended automation support,
feel free to open an issue or propose an enhancement.
Author: Mad Brains
License: MIT
Libraries
- Support for doing something awesome.