makeTextPainter function

TextPainter makeTextPainter(
  1. String text,
  2. TextStyle style
)

Constructs a text painter and performs layout.

Use this in combination with paintWithTextPainter, if you need to know text size on the canvas.

e.g.

final tp = makeTextPainter('Hello', style);
final w = tp.width; // text width
final h = tp.height; // text height

paintWithTextPainter(
  canvas,
  painter: tp,
  anchor: Offset(0, 0),
  anchorAlignment: Alignment.centerRight,
);

Implementation

TextPainter makeTextPainter(String text, TextStyle style) {
  final TextSpan span = TextSpan(text: text, style: style);
  return TextPainter(text: span, textDirection: TextDirection.ltr)..layout();
}