generate method
Implementation
@override
String? generate() {
final hasColorTokens =
themes.any((theme) => theme.resolvedTokens.any((token) => token.type == 'color'));
if (!hasColorTokens) return null;
var output = '';
output += _genWarning;
output += "\n\n import 'package:flutter/material.dart'; \n\n";
final primaryTheme = themes.first;
output += 'class AppColorsExtension extends ThemeExtension<AppColorsExtension> {\n';
output += ' const AppColorsExtension({\n';
// Generate properties
_processTokens(
theme: primaryTheme,
type: 'color',
callback: (token) => output += ' required this.${token.variableName},\n',
);
output += ' });\n\n';
// Generate properties again for final declaration
_processTokens(
theme: primaryTheme,
type: 'color',
callback: (token) => output += ' final Color ${token.variableName};\n',
);
// Generate copyWith method
output += '\n @override\n';
output += ' ThemeExtension<AppColorsExtension> copyWith({\n';
_processTokens(
theme: primaryTheme,
type: 'color',
callback: (token) => output += ' Color? ${token.variableName},\n',
);
output += ' }) {\n';
output += ' return AppColorsExtension(\n';
_processTokens(
theme: primaryTheme,
type: 'color',
callback: (token) => output +=
' ${token.variableName}: ${token.variableName} ?? this.${token.variableName},\n',
);
output += ' );\n';
output += ' }\n\n';
// Generate lerp method
output += ' @override\n';
output += ' ThemeExtension<AppColorsExtension> lerp(\n';
output += ' covariant ThemeExtension<AppColorsExtension>? other,\n';
output += ' double t,\n';
output += ' ) {\n';
output += ' if (other is! AppColorsExtension) {\n';
output += ' return this;\n';
output += ' }\n\n';
output += ' return AppColorsExtension(\n';
_processTokens(
theme: primaryTheme,
type: 'color',
callback: (token) => output +=
' ${token.variableName}: Color.lerp(${token.variableName}, other.${token.variableName}, t)!,\n',
);
output += ' );\n';
output += ' }\n';
output += '}\n\n';
final colorTransformer = ColorTransformer();
// Generate AppColorsExtension instances for primary and secondary themes
output += 'const primaryAppColors = AppColorsExtension(\n';
_processTokens(
theme: primaryTheme,
type: 'color',
callback: (token) {
final colorDeclaration = colorTransformer.transform(token);
output += ' ${token.variableName}: $colorDeclaration, \n';
},
);
output += ');\n\n';
if (themes.elementAtOrNull(1) != null) {
output += 'const secondaryAppColors = AppColorsExtension(\n';
_processTokens(
theme: themes[1],
type: 'color',
callback: (token) {
final colorDeclaration = colorTransformer.transform(token);
output += ' ${token.variableName}: $colorDeclaration, \n';
},
);
output += ');\n';
}
return output;
}