getTextHeight function
double
getTextHeight(
- BuildContext context,
- String text,
- TextStyle style,
- double maxWidth,
- int? maxLines, {
- TextDirection? direction,
- 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;
}