generate method

  1. @override
Future<String?> generate(
  1. LibraryReader library,
  2. BuildStep buildStep
)

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';

  // 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')),
      );
    }
  }
  ''';

  const signupScreenContent = '''
  // This is the signup screen.

  import 'package:flutter/material.dart';

  class SignupScreen extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(title: Text('Sign Up')),
        body: Center(child: Text('Signup Screen')),
      );
    }
  }
  ''';

  const splashScreenContent = '''
  // This is the splash screen.

  import 'package:flutter/material.dart';

  class SplashScreen extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: Center(child: Text('Splash Screen')),
      );
    }
  }
  ''';

  // Generate each file in the specified directory structure.
  await buildStep.writeAsString(
    AssetId(buildStep.inputId.package, '$featurePath/loginScreen.dart'),
    loginScreenContent,
  );

  await buildStep.writeAsString(
    AssetId(buildStep.inputId.package, '$featurePath/signupScreen.dart'),
    signupScreenContent,
  );

  await buildStep.writeAsString(
    AssetId(buildStep.inputId.package, '$featurePath/splashScreen.dart'),
    splashScreenContent,
  );
  return null;
}