getTextHeight function

double getTextHeight(
  1. BuildContext context,
  2. String text,
  3. TextStyle style,
  4. double maxWidth,
  5. int? maxLines, {
  6. TextDirection? direction,
  7. double bufferSpacing = 5,
})

Calculates the height required to display text within given constraints.

Implementation

double getTextHeight(
  BuildContext context,
  String text,
  TextStyle style,
  double maxWidth,
  int? maxLines, {
  TextDirection? direction,
  double bufferSpacing = 5,
}) {
  final span = TextSpan(text: text, style: style);
  final tp = TextPainter(
    text: span,
    textDirection: direction ?? Directionality.of(context),
    textScaler: MediaQuery.of(context).textScaler,
    maxLines: maxLines,
  );
  tp.layout(minWidth: 0, maxWidth: maxWidth);
  final lines = tp.computeLineMetrics().length;
  return tp.size.height +
      (lines * (style.fontSize! * (style.height ?? 1) - style.fontSize!)) +
      bufferSpacing;
}