calculateTextHeight static method

double calculateTextHeight(
  1. BuildContext context,
  2. String? value,
  3. double fontSize,
  4. FontWeight fontWeight,
  5. double maxWidth,
  6. int maxLines,
)

计算文本高度

Implementation

static double calculateTextHeight(BuildContext context, String? value,
    double fontSize, FontWeight fontWeight, double maxWidth, int maxLines) {
  if (value == null || value.isEmpty) {
    return 0;
  }
  //创建painter
  TextPainter painter = TextPainter(
    locale: Localizations.maybeLocaleOf(context),
    maxLines: maxLines,
    textDirection: TextDirection.ltr,
    text: TextSpan(
      text: value,
      style: TextStyle(
        fontWeight: fontWeight,
        fontSize: fontSize,
      ),
    ),
  );
  painter.layout(maxWidth: maxWidth);
  return painter.height;
}