generate method
Executes the plugin's code-generation and configuration logic.
Implementation
@override
Future<void> generate(ProjectContext context) async {
final pubspecPath = '${context.projectPath}/pubspec.yaml';
// 1. Add Riverpod dependencies
Services.yamlService.addDependency(pubspecPath, 'flutter_riverpod', '^2.5.1');
Services.yamlService.addDependency(pubspecPath, 'riverpod_annotation', '^2.3.3');
// Add Riverpod generators for build_runner
Services.yamlService.addDependency(pubspecPath, 'riverpod_generator', '^2.3.9', isDev: true);
Services.yamlService.addDependency(pubspecPath, 'build_runner', '^2.4.8', isDev: true);
// 2. Create providers folder
Services.fileService.createDir('${context.projectPath}/lib/core/providers');
// 3. Render example provider template
await Services.templateService.renderTemplate(
templateSubPath: 'state_management/counter_provider.dart',
targetPath: '${context.projectPath}/lib/core/providers/counter_provider.dart',
replacements: {},
);
// 4. Register Main entry replacements for Riverpod
final currentImports = context.templateReplacements['STATE_MANAGEMENT_IMPORTS'] ?? '';
context.templateReplacements['STATE_MANAGEMENT_IMPORTS'] =
"${currentImports}import 'package:flutter_riverpod/flutter_riverpod.dart';\n";
context.templateReplacements['STATE_MANAGEMENT_WRAPPER_START'] = "ProviderScope(\n child: ";
context.templateReplacements['STATE_MANAGEMENT_WRAPPER_END'] = "\n )";
// Update Splash & Onboarding class templates to use Consumer versions
context.templateReplacements['STATE_MANAGEMENT_IMPORT'] =
"import 'package:flutter_riverpod/flutter_riverpod.dart';\n";
context.templateReplacements['SPLASH_WIDGET_BASE_CLASS'] = 'ConsumerStatefulWidget';
context.templateReplacements['SPLASH_STATE_BASE_CLASS'] = 'ConsumerState<SplashView>';
context.templateReplacements['SPLASH_STATE_BASE_CLASS_SHORT'] = 'ConsumerState<SplashView>';
context.templateReplacements['ONBOARDING_WIDGET_BASE_CLASS'] = 'ConsumerStatefulWidget';
context.templateReplacements['ONBOARDING_STATE_BASE_CLASS'] = 'ConsumerState<OnboardingView>';
context.templateReplacements['ONBOARDING_STATE_BASE_CLASS_SHORT'] = 'ConsumerState<OnboardingView>';
}