cssFontWeight function

FontWeight cssFontWeight(
  1. int css
)

css (clamped to 100–900) as a FontWeight: exact on Flutter 3.38+, which has arbitrary weights (variable wght); the nearest of FontWeight.values on older Flutter. FontWeight(int) doesn't exist there, but FontWeight.lerp does, and is exact where weights are.

Implementation

FontWeight cssFontWeight(int css) {
  final c = css.clamp(100, 900);
  final below = FontWeight.values[(c - 100) ~/ 100];
  if (below.value == c) return below;
  final above = FontWeight.values[(c - 100) ~/ 100 + 1];
  return FontWeight.lerp(below, above, (c - below.value) / 100)!;
}