generate method
Executes the plugin's code-generation and configuration logic.
Implementation
@override
Future<void> generate(ProjectContext context) async {
final hexVal = _formatHexToColorVal(context.primaryColor);
// 1. Create the AppTheme file using our template
await Services.templateService.renderTemplate(
templateSubPath: 'styling/app_theme.dart',
targetPath: '${context.projectPath}/lib/core/theme/app_theme.dart',
replacements: {
'PRIMARY_COLOR_VALUE': hexVal,
'FONT_FAMILY': context.customFont ?? 'Outfit',
},
);
// 2. Register styling imports in main.dart
final currentImports = context.templateReplacements['THEME_IMPORTS'] ?? '';
context.templateReplacements['THEME_IMPORTS'] = "${currentImports}import 'core/theme/app_theme.dart';\n";
// 3. Update the default MaterialApp configuration if router is not overriding it
if (context.routing != Routing.navigator && !context.useGoRouter && !context.useNavigator) {
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'] = '''
MaterialApp(
title: '${context.projectName}',
$themeConfig
home: const Scaffold(
body: Center(
child: Text('Welcome to ${context.projectName}!'),
),
),
)''';
}
}