paint method
Paint this render object into the given context at the given offset.
Subclasses should override this method to provide a visual appearance for themselves. The render object's local coordinate system is axis-aligned with the coordinate system of the context's canvas and the render object's local origin (i.e, x=0 and y=0) is placed at the given offset in the context's canvas.
Do not call this function directly. If you wish to paint yourself, call
markNeedsPaint instead to schedule a call to this function. If you wish
to paint one of your children, call PaintingContext.paintChild on the
given context.
When painting one of your children (via a paint child function on the given context), the current canvas held by the context might change because draw operations before and after painting children might need to be recorded on separate compositing layers.
Implementation
@override
void paint(PaintingContext context, Offset offset) {
final canvas = context.canvas;
canvas.save();
canvas.translate(offset.dx, offset.dy);
if (_staticPicture == null || _staticSize != size) {
_rebuildStaticPicture(size);
}
canvas.drawPicture(_staticPicture!);
final frac = valueToFraction(_controller.value, _min, _max);
if (frac > 0) {
_paintGlow(canvas);
_paintBar(canvas, Paint()..color = _tokens.valueColor);
}
// Value label above the bar tip
if (_showValue && frac > 0) {
final trackW = _tokens.trackStrokeWidth;
final isH = _isHorizontal;
final effectiveFrac = _reverse ? 1.0 - frac : frac;
final label = _fmtWithUnit(_controller.value);
final tp = TextPainter(
text: TextSpan(text: label, style: _tokens.labelStyle),
textDirection: TextDirection.ltr,
)..layout();
final rec = ui.PictureRecorder();
tp.paint(Canvas(rec), Offset.zero);
final pic = rec.endRecording();
canvas.save();
if (isH) {
final left = trackW / 2 + 4;
final right = size.width - trackW / 2 - 4;
final cy = size.height / 2;
final trackLen = right - left;
final tipX = _reverse
? left + effectiveFrac * trackLen
: left + effectiveFrac * trackLen;
canvas.translate(
tipX - tp.width / 2,
cy - trackW / 2 - tp.height - 2,
);
} else {
final top = trackW / 2 + 4;
final bottom = size.height - trackW / 2 - 4;
final cx = size.width / 2;
final trackLen = bottom - top;
final tipY = top + effectiveFrac * trackLen;
canvas.translate(cx + trackW / 2 + 4, tipY - tp.height / 2);
}
canvas.drawPicture(pic);
canvas.restore();
}
canvas.restore();
}