How Do You Use A TextPainter To Draw Text?

- 1 answer

The documentation for CustomPainter's paint method says, "To paint text on a Canvas, use a TextPainter". So within my MyCustomPainter's paint method I have the following:

TextSpan span = new TextSpan(text: 'Yrfc');
TextPainter tp = new TextPainter(text: span, textAlign: TextAlign.left);
tp.paint(canvas, new Offset(5.0, 5.0));

I've tried a variety of offsets (, Offset.infinite, new Offset(10.0, 10.0) but I never am able to see the text painted on the screen.



I found the answer as I was typing up this question but I've been wrestling with it for a while now, so posting in case it helps anyone else.

What solved it was changing the TextSpan line to:

TextSpan span = new TextSpan(style: new TextStyle(color: Colors.grey[600]), text: 'Yrfc');

Apparently it was either drawing the text invisibly or as white (background) color since I hadn't made my color choice explicit.