paint method
Paints this node to the canvas.
Subclasses, such as Sprite, override this method to do the actual
painting of the node. To do custom drawing override this method and make
calls to the canvas
object. All drawing is done in the node's local
coordinate system, relative to the node's position. If you want to make
the drawing relative to the node's bounding box's origin, override
NodeWithSize and call the applyTransformForPivot method before making
calls for drawing.
void paint(Canvas canvas) {
canvas.save();
applyTransformForPivot(canvas);
// Do painting here
canvas.restore();
}
Implementation
@override
void paint(Canvas canvas) {
if (_painter == null) {
_painter = TextPainter(
text: TextSpan(style: _textStyle, text: _text),
textDirection: TextDirection.ltr,
)..layout();
_width = _painter!.size.width;
}
Offset offset = Offset.zero;
if (textAlign == TextAlign.center) {
offset = Offset(-_width / 2.0, 0.0);
} else if (textAlign == TextAlign.right) {
offset = Offset(-_width, 0.0);
}
_painter!.paint(canvas, offset);
}