buildTokenSet function
Generates theming for all available token sets.
Theming includes:
ColorScheme
TextStyle
ThemeData
with all extensions
Implementation
String buildTokenSet(
Map<String, dynamic> tokens, {
required BuilderConfig config,
}) {
var output = '';
final tokenSets = getTokenSets(tokens, config: config);
final defaultSetData = tokens[config.defaultSetName] as Map<String, dynamic>;
final defaultSys = defaultSetData['sys'] as Map<String, dynamic>;
for (final tokenSet in tokenSets) {
final setData = tokens[tokenSet] as Map<String, dynamic>;
var colorScheme = '';
var textTheme = '';
var brightness = _brightness(tokenSet: tokenSet);
final sys = setData.remove('sys') as Map<String, dynamic>?;
if (sys != null) {
/// Generate color scheme.
final systemColors = getTokensOfType(
'color',
tokenSetData: sys,
fallbackSetData: defaultSys,
);
if (systemColors.isNotEmpty) {
final colorSchemeValues = systemColors.keys.map(
(key) => '$key: ${_parseAttribute(
sys[key],
config: config,
isConst: false,
)}',
);
colorScheme +=
'ColorScheme get _colorScheme => const ColorScheme.$brightness(\n${indentation(
level: 4,
)}${colorSchemeValues.join(
',\n${indentation(level: 4)}',
)},\n${indentation(level: 3)});';
}
/// Generate text style.
var systemTextTheme = getTokensOfType(
'typography',
tokenSetData: sys,
fallbackSetData: defaultSys,
);
systemTextTheme = prepareTypographyTokens(systemTextTheme);
if (systemTextTheme.isNotEmpty) {
final textThemeValues = systemTextTheme.keys.map(
(key) => '$key: ${_parseAttribute(
systemTextTheme[key],
config: config,
indentationLevel: 4,
isConst: false,
)}',
);
textTheme +=
'TextTheme get _textTheme => const TextTheme(\n${indentation(
level: 4,
)}${textThemeValues.join(
',\n${indentation(level: 4)}',
)},\n${indentation(level: 3)});';
}
}
final extensions = getExtensions(tokens, config: config);
var themeData = '''@override
ThemeData get themeData => ThemeData.$brightness().copyWith(
colorScheme: _colorScheme,
textTheme: _textTheme,
extensions: [
${extensions.keys.map(
(e) => '${buildExtensionName(
e,
)}(\n${indentation(level: 6)}${extensions[e]!.map(
(e) => '${e.item1}: ${_parseAttribute(
e.item2,
config: config,
indentationLevel: 6,
)}',
).join(
',\n${indentation(level: 6)}',
)},\n${indentation(level: 5)})',
).join(
',\n${indentation(level: 4)}',
)},
],
);''';
output +=
'''class ${tokenSet.firstUpperCased}ThemeData with GeneratedThemeData {
const ${tokenSet.firstUpperCased}ThemeData();
$colorScheme
$textTheme
$themeData
}
''';
}
return '$output${generateTokenSetEnum(tokenSets, config: config)}';
}