Sub topic 11
Drawing Text with TextPainter
This chapter assumes you know Dart classes, Flutter widget trees, Size, and basic CustomPainter. A TextPainter is a Flutter object that measures and draws text onto a Canvas. It is useful when text must appear inside custom graphics, such as chart labels, badges, diagrams, or a painted button.
Layout before painting
Canvas drawing uses coordinates in logical pixels. The top-left corner of the painter is (0, 0). Before TextPainter can draw text, it must calculate the text's size by calling layout().
The usual sequence is:
- Create a
TextSpancontaining the text and its style. - Create a
TextPainterwith that span and a text direction. - Call
layout(). - Calculate the text's position using
textPainter.widthandtextPainter.height. - Call
paint().
TextDirection.ltr means left-to-right text. Use TextDirection.rtl for right-to-left text, or obtain the direction from the surrounding Flutter context.
import 'package:flutter/material.dart';
class LabelPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final textPainter = TextPainter(
text: const TextSpan(
text: '42%',
style: TextStyle(
color: Colors.white,
fontSize: 28,
fontWeight: FontWeight.bold,
),
),
textDirection: TextDirection.ltr,
textAlign: TextAlign.center,
)..layout(maxWidth: size.width);
final offset = Offset(
(size.width - textPainter.width) / 2,
(size.height - textPainter.height) / 2,
);
textPainter.paint(canvas, offset);
}
@override
bool shouldRepaint(covariant LabelPainter oldDelegate) => false;
}The maxWidth argument limits the line width. If the text is longer than this width, it can wrap onto multiple lines. Without a width limit, textAlign has little visible effect because the text is laid out only as wide as its contents.
Rich text and baseline positioning
A TextSpan can contain child spans with different styles. This allows a painter to draw a value and its unit as one measured block.
Text is positioned by its top-left bounding box when you use textPainter.paint(). For precise alignment with lines or other labels, use textPainter.computeLineMetrics(). Each line metric includes an ascent, descent, and baseline. The baseline is the horizontal line on which most letters sit.
final painter = TextPainter(
text: const TextSpan(
children: [
TextSpan(
text: '80',
style: TextStyle(fontSize: 32, color: Colors.black),
),
TextSpan(
text: ' km/h',
style: TextStyle(fontSize: 16, color: Colors.grey),
),
],
),
textDirection: TextDirection.ltr,
)..layout();
painter.paint(canvas, const Offset(20, 20));Text constraints and repainting
Call layout() again whenever the available width, text, or style changes. Keep the TextPainter local to paint() unless you have a clear reason to cache it. If the painter receives text or styling as fields, compare those fields in shouldRepaint.
Exercise
Why must layout be called before TextPainter.paint, and what does maxWidth control?
Show answer
layout calculates the text's size and line breaks before painting. maxWidth limits the available line width and allows long text to wrap.