parseCssFontWeight function

FontWeight? parseCssFontWeight(
  1. String? value
)

Parses a CSS font-weight, numeric or keyword.

Implementation

FontWeight? parseCssFontWeight(String? value) {
  if (value == null) return null;
  final input = value.trim().toLowerCase();
  switch (input) {
    case 'normal':
      return FontWeight.w400;
    case 'bold':
      return FontWeight.w700;
    case 'lighter':
      return FontWeight.w300;
    case 'bolder':
      return FontWeight.w800;
  }
  final numeric = int.tryParse(input);
  if (numeric == null) return null;
  // Snap to the nearest supported weight rather than rejecting 350.
  final index = ((numeric / 100).round() - 1).clamp(0, 8);
  return FontWeight.values[index];
}