fitFontSize static method

double fitFontSize(
  1. String text,
  2. TextStyle style,
  3. Size bounds, {
  4. double minFontSize = 8,
  5. double maxFontSize = 24,
})

Calculates the optimal font size to fit text within bounds.

Implementation

static double fitFontSize(
  String text,
  TextStyle style,
  Size bounds, {
  double minFontSize = 8,
  double maxFontSize = 24,
}) {
  double fontSize = maxFontSize;

  while (fontSize >= minFontSize) {
    final size = measureText(text, style.copyWith(fontSize: fontSize));
    if (size.width <= bounds.width && size.height <= bounds.height) {
      return fontSize;
    }
    fontSize -= 1;
  }

  return minFontSize;
}