generate method

  1. @override
String? generate()
override

Implementation

@override
String? generate() {
  final hasTypographyTokens =
      primaryTheme.resolvedTokens.any((token) => token.type == 'typography');

  if (!hasTypographyTokens) return null;

  var output = '';

  output += _genWarning;

  output += "\n\nimport 'package:flutter/material.dart'; \n\n";

  output += 'abstract class AppTypography {\n';

  final typographyTransformer = TypographyTransformer();
  // Generate static final properties
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) {
      output +=
          '  static const ${token.variableName} = ${typographyTransformer.transform(token)};\n';
    },
  );

  output += '\n  static const textTheme = AppTextThemeExtension(\n';
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) =>
        output += '    ${token.variableName}: AppTypography.${token.variableName},\n',
  );

  output += '  );\n';

  output += '\n  static AppTextThemeExtension applyTextColor(Color color) {\n';
  output += '    return AppTextThemeExtension(\n';

  // Generate copyWith method calls
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) =>
        output += '      ${token.variableName}: ${token.variableName}.copyWith(color: color),\n',
  );

  output += '    );\n';
  output += '  }\n';
  output += '}\n';

  output += 'class AppTextThemeExtension extends ThemeExtension<AppTextThemeExtension> {\n';
  output += '  const AppTextThemeExtension({\n';

  // Generate required properties
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) => output += '    required this.${token.variableName},\n',
  );

  output += '  });\n\n';

  // Generate final properties
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) => output += '  final TextStyle? ${token.variableName};\n',
  );

  // Generate copyWith method
  output += '\n  @override\n';
  output += '  ThemeExtension<AppTextThemeExtension> copyWith({\n';
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) => output += '    TextStyle? ${token.variableName},\n',
  );
  output += '  }) {\n';
  output += '    return AppTextThemeExtension(\n';
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) => output +=
        '      ${token.variableName}: ${token.variableName} ?? this.${token.variableName},\n',
  );
  output += '    );\n';
  output += '  }\n\n';

  // Generate lerp method
  output += '  @override\n';
  output += '  ThemeExtension<AppTextThemeExtension> lerp(\n';
  output += '    covariant ThemeExtension<AppTextThemeExtension>? other,\n';
  output += '    double t,\n';
  output += '  ) {\n';
  output += '    if (other is! AppTextThemeExtension) {\n';
  output += '      return this;\n';
  output += '    }\n\n';
  output += '    return AppTextThemeExtension(\n';
  _processTokens(
    theme: primaryTheme,
    type: 'typography',
    callback: (token) => output +=
        '      ${token.variableName}: TextStyle.lerp(${token.variableName}, other.${token.variableName}, t),\n',
  );
  output += '    );\n';
  output += '  }\n';

  output += '}\n';

  return output;
}