generate method
Generates Dart code for an input Dart library.
May create additional outputs through the buildStep
, but the 'primary'
output is Dart code returned through the Future. If there is nothing to
generate for this library may return null, or a Future that resolves to
null or the empty string.
Implementation
@override
Future<String?> generate(LibraryReader library, BuildStep buildStep) async {
// Define the path for the generated folder structure.
const featurePath = 'lib/features/authentication';
// Create the directory if it doesn't exist
final dir = Directory(featurePath);
if (!await dir.exists()) {
await dir.create(recursive: true);
}
// Define file contents for each screen.
const loginScreenContent = '''
// This is the login screen.
import 'package:flutter/material.dart';
class LoginScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Login')),
body: Center(child: Text('Login Screen')),
);
}
}
''';
// Generate each file in the specified directory structure.
await buildStep.writeAsString(
AssetId(buildStep.inputId.package, '$featurePath/loginScreen.dart'),
loginScreenContent,
);
return null; // Return null at the end
}