generate method
Executes the plugin's code-generation and configuration logic.
Implementation
@override
Future<void> generate(ProjectContext context) async {
// Only execute if standard Navigator is chosen
if (context.routing != Routing.navigator) return;
// 1. Render route_names.dart template
await Services.templateService.renderTemplate(
templateSubPath: 'routing/route_names.dart',
targetPath: '${context.projectPath}/lib/core/router/route_names.dart',
replacements: {},
);
// 2. Build route templates & imports based on Splash/Onboarding selection
String pageImports = '';
String routesList = '';
if (context.includeSplashOnboarding) {
pageImports = "${context.splashViewImport}${context.onboardingViewImport}${context.homeViewImport}";
routesList = '''RouteNames.onboarding: (context) => const OnboardingView(),
RouteNames.home: (context) => const HomeView(),''';
} else {
pageImports = context.homeViewImport;
routesList = '''RouteNames.home: (context) => const HomeView(),''';
}
// 3. Render navigator_router.dart template
await Services.templateService.renderTemplate(
templateSubPath: 'routing/navigator_router.dart',
targetPath: '${context.projectPath}/lib/core/router/router.dart',
replacements: {
'PROJECT_NAME': context.projectName,
'NAVIGATOR_PAGE_IMPORTS': pageImports,
'NAVIGATOR_ROUTES_LIST': routesList,
},
);
// 4. Register route imports in main.dart
final currentImports = context.templateReplacements['ROUTER_IMPORTS'] ?? '';
context.templateReplacements['ROUTER_IMPORTS'] =
"${currentImports}import 'core/router/router.dart';\nimport 'core/router/route_names.dart';\n";
// 5. Configure navigation actions for Splash & Onboarding to use pushReplacementNamed
context.templateReplacements['ROUTER_IMPORT'] = "import '${context.coreRouterRelativePath}route_names.dart';\n";
context.templateReplacements['NAVIGATE_HOME'] = "Navigator.of(context).pushReplacementNamed(RouteNames.home);";
context.templateReplacements['NAVIGATE_ONBOARDING'] = "Navigator.of(context).pushReplacementNamed(RouteNames.onboarding);";
context.templateReplacements['HOME_VIEW_IMPORT'] = '';
context.templateReplacements['SPLASH_HOME_IMPORT'] = '';
context.templateReplacements['SPLASH_ONBOARDING_IMPORT'] = '';
// 6. Register MaterialApp/CupertinoApp named routes configuration in main.dart
final isCupertino = context.theme == UITheme.cupertino;
final initialRouteKey = context.includeSplashOnboarding ? 'RouteNames.splash' : 'RouteNames.home';
final appClass = context.useGetX
? (isCupertino ? 'GetCupertinoApp' : 'GetMaterialApp')
: (isCupertino ? 'CupertinoApp' : 'MaterialApp');
if (isCupertino) {
context.templateReplacements['APP_ROUTER_OR_MATERIAL_APP'] = '''
$appClass(
title: '${context.projectName}',
theme: AppTheme.theme,
initialRoute: $initialRouteKey,
routes: AppRouter.routes,
localizationsDelegates: const [
DefaultMaterialLocalizations.delegate,
DefaultWidgetsLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
)''';
} else {
final themeConfig = context.darkMode
? '''theme: AppTheme.lightTheme,
darkTheme: AppTheme.darkTheme,
themeMode: ThemeMode.system,'''
: '''theme: AppTheme.lightTheme,
themeMode: ThemeMode.light,''';
context.templateReplacements['APP_ROUTER_OR_MATERIAL_APP'] = '''
$appClass(
title: '${context.projectName}',
$themeConfig
initialRoute: $initialRouteKey,
routes: AppRouter.routes,
)''';
}
}