messureText method

double messureText(
  1. Characters? text, {
  2. double? width,
  3. TextStyle? style,
})

Implementation

double messureText(Characters? text, {double? width, TextStyle? style}) {
  if (isNullOrEmpty(text)) return 0;
  style ??= _defaultStyle;
  double sumWidth = 0;
  double? currentWidth;
  double maxWidth = 0;
  for (var ch in text!) {
    currentWidth = cachedCharWidth[ch];
    if (currentWidth == null) {
      textPainter.text = TextSpan(
        text: ch,
        style: style,
      );
      textPainter.layout(
          maxWidth: currentWidth ?? double.infinity, minWidth: 0);
      currentWidth = textPainter.size.width;
      cachedCharWidth[ch] = currentWidth;
    }

    sumWidth += currentWidth;
    if (width != null && sumWidth >= width) {
      return width;
    }
    if (ch == '\n') {
      if (maxWidth < sumWidth) {
        maxWidth = sumWidth;
      }
      sumWidth = 0;
    }
  }
  return max(maxWidth, sumWidth);
}