paint method Null safety

void paint(
  1. Canvas canvas,
  2. Offset offset
)

Paints the text onto the given canvas at the given offset.

Valid only after layout has been called.

If you cannot see the text being painted, check that your text color does not conflict with the background on which you are drawing. The default text color is white (to contrast with the default black background color), so if you are writing an application with a white background, the text will not be visible by default.

To set the text style, specify a TextStyle when creating the TextSpan that you pass to the TextPainter constructor or to the text property.

Implementation

void paint(Canvas canvas, Offset offset) {
  final double? minWidth = _lastMinWidth;
  final double? maxWidth = _lastMaxWidth;
  if (_paragraph == null || minWidth == null || maxWidth == null) {
    throw StateError(
      'TextPainter.paint called when text geometry was not yet calculated.\n'
      'Please call layout() before paint() to position the text before painting it.',
    );
  }

  if (_rebuildParagraphForPaint) {
    Size? debugSize;
    assert(() {
      debugSize = size;
      return true;
    }());

    _createParagraph();
    // Unfortunately we have to redo the layout using the same constraints,
    // since we've created a new ui.Paragraph. But there's no extra work being
    // done: if _needsPaint is true and _paragraph is not null, the previous
    // `layout` call didn't invoke _layoutParagraph.
    _layoutParagraph(minWidth, maxWidth);
    assert(debugSize == size);
  }
  canvas.drawParagraph(_paragraph!, offset);
}