CarpenterType.fromConfig constructor

CarpenterType.fromConfig({
  1. required CarpenterTypeConfig config,
  2. required double baseSize,
})

Создает typography runtime из config.

Implementation

factory CarpenterType.fromConfig({
  required CarpenterTypeConfig config,
  required double baseSize,
}) {
  final base = config.baseSize ?? baseSize;
  final family = config.fontFamily;
  final fallback = config.fontFamilyFallback;
  final package = config.package;

  TextStyle style({
    required double size,
    required double height,
    FontWeight? weight,
  }) {
    return TextStyle(
      fontFamily: family,
      fontFamilyFallback: fallback,
      package: package,
      fontSize: size,
      height: height,
      fontWeight: weight,
    );
  }

  final ratio = config.scaleRatio;
  final tokens = <String, TextStyle>{
    'body': style(size: base, height: 1.5),
    'body.strong': style(size: base, height: 1.5, weight: FontWeight.w600),
    'label': style(size: base / ratio, height: 1.25),
    'label.strong': style(
      size: base / ratio,
      height: 1.25,
      weight: FontWeight.w600,
    ),
    'caption': style(size: base / ratio / ratio, height: 1.25),
    'label.primary': style(size: base / ratio, height: 1.25),
    'label.primary.strong': style(
      size: base / ratio,
      height: 1.25,
      weight: FontWeight.w600,
    ),
    'caption.primary': style(size: base / ratio / ratio, height: 1.25),
    'title': style(
      size: base * ratio * ratio,
      height: 1.25,
      weight: FontWeight.w600,
    ),
    'subtitle': style(
      size: base * ratio,
      height: 1.25,
      weight: FontWeight.w600,
    ),
  };

  tokens.addAll(config.tokens);
  final secondaryFamily = config.secondaryFontFamily;
  final secondaryTokens = secondaryFamily == null
      ? const <String, TextStyle>{}
      : {
          for (final entry in tokens.entries)
            entry.key: entry.value.copyWith(
              fontFamily: secondaryFamily,
              fontFamilyFallback:
                  config.secondaryFontFamilyFallback ?? fallback,
              package: config.secondaryPackage,
            ),
        };
  return CarpenterType(tokens, secondaryTokens: secondaryTokens);
}