generateFonts function

String generateFonts(
  1. FontsGenConfig config,
  2. DartFormatter formatter
)

Implementation

String generateFonts(
  FontsGenConfig config,
  DartFormatter formatter,
) {
  final fonts = config.fonts;
  final fontsConfig = config.flutterGen.fonts;
  if (fonts.isEmpty) {
    throw InvalidSettingsException(
        'The value of "flutter/fonts:" is incorrect.');
  }

  final buffer = StringBuffer();
  final className = fontsConfig.outputs.className;
  buffer.writeln(header);
  buffer.writeln(ignore);
  buffer.writeln('class $className {');
  buffer.writeln('$className._();');
  buffer.writeln();

  final isPackage = config.packageParameterLiteral.isNotEmpty;
  if (isPackage) {
    buffer.writeln("static const String package = '${config._packageName}';");
    buffer.writeln();
  }

  fonts.map((element) => element.family).distinct().sorted().forEach((family) {
    final keyName = isPackage ? 'packages/\$package/$family' : family;
    buffer.writeln("""/// Font family: $family
    static const String ${family.camelCase()} = '$keyName';""");
  });

  buffer.writeln('}');
  return formatter.format(buffer.toString());
}