calculateTextSize function

Size calculateTextSize({
  1. required String text,
  2. required TextStyle? style,
  3. required BuildContext context,
  4. double textScaleFactor = 1.0,
})

Implementation

Size calculateTextSize({
  required String text,
  required TextStyle? style,
  required BuildContext context,
  double textScaleFactor = 1.0,
}) {
  final defaultTextStyle = DefaultTextStyle.of(context);
  style = defaultTextStyle.style.merge(style);

  final textPainter = TextPainter(
    text: TextSpan(
      text: text,
      style: style,
    ),
    textDirection: TextDirection.ltr,
  )..layout();

  return textPainter.size;
}