fromJson method

  1. @override
FontStyleTokenValue fromJson(
  1. Object? value
)

Implementation

@override
FontStyleTokenValue fromJson(Object? value) {
  if (value is Map<String, dynamic>) {
    final fontSize = value['fontSize'];
    final fontFamily = value['fontFamily'];
    final fontWeight = value['fontWeight'];
    final textDecoration = value['textDecoration'];
    final fontStyle = value['fontStyle'];
    final letterSpacing = value['letterSpacing'];
    final lineHeight = value['lineHeight'];

    final effectiveFontSize = fontSize is num ? fontSize.toDouble() : 12.0;

    return FontStyleTokenValue(
      textStyle: TextStyle(
        fontSize: effectiveFontSize,
        fontFamily: fontFamily is String ? fontFamily : null,
        letterSpacing: letterSpacing is num ? letterSpacing.toDouble() : 0.0,
        height: fontFamily is num
            ? (lineHeight.toDouble() / effectiveFontSize).toDouble()
            : null,
        fontWeight: fontWeight is num
            ? FontWeight.values.firstWhere(
                (value) => (value.index + 1) * 100 == fontWeight,
                orElse: () => FontWeight.w400,
              )
            : FontWeight.w400,
        decoration: () {
          switch (textDecoration) {
            case 'line-through':
              return TextDecoration.lineThrough;
            case 'underline':
              return TextDecoration.underline;
            default:
              return TextDecoration.none;
          }
        }(),
        fontStyle: () {
          switch (fontStyle) {
            case 'italic':
              return FontStyle.italic;
            default:
              return FontStyle.normal;
          }
        }(),
      ),
    );
  }
  return const FontStyleTokenValue(
    textStyle: TextStyle(),
  );
}