paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset offset
)
override

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!);

  // Dynamic: value arc
  final center = size.center(Offset.zero);
  final radius = size.shortestSide / 2 - _tokens.trackStrokeWidth / 2 - 2;
  final startRad = degToRad(_startAngleDeg);
  final sweepRad = degToRad(_sweepAngleDeg);
  final valueAngle =
      valueToAngle(_controller.value, _min, _max, startRad, sweepRad);

  if (valueAngle > startRad) {
    final valueSweep = valueAngle - startRad;
    final rect = Rect.fromCircle(center: center, radius: radius);

    if (_tokens.valueGradient != null) {
      final shader = _tokens.valueGradient!.createShader(rect);
      final vPaint = Paint()
        ..shader = shader
        ..style = PaintingStyle.stroke
        ..strokeWidth = _tokens.valueStrokeWidth
        ..strokeCap = _tokens.trackStrokeCap;
      canvas.drawArc(rect, startRad, valueSweep, false, vPaint);
    } else {
      final vPaint = Paint()
        ..color = _tokens.valueColor
        ..style = PaintingStyle.stroke
        ..strokeWidth = _tokens.valueStrokeWidth
        ..strokeCap = _tokens.trackStrokeCap;
      canvas.drawArc(rect, startRad, valueSweep, false, vPaint);
    }
  }

  // Needle
  if (_showNeedle) {
    _paintNeedle(canvas, center, radius, valueAngle);
  }

  // Extra pointers — drawn after the main needle so they appear on top.
  for (final pointer in _extraPointers) {
    final pointerAngle = valueToAngle(
        pointer.controller.value, _min, _max, startRad, sweepRad);
    _paintExtraNeedle(canvas, center, radius, pointerAngle, pointer, _tokens);
  }

  // Center label — painted via PictureRecorder so text renders correctly in CanvasKit
  if (_showCenterLabel) {
    final labelText = _centerLabel ?? _formatCenter(_controller.value);
    final tp = TextPainter(
      text: TextSpan(
        text: labelText,
        style: _centerLabelStyle ??
            _tokens.labelStyle.copyWith(
              fontSize: 22,
              fontWeight: FontWeight.bold,
            ),
      ),
      textDirection: TextDirection.ltr,
    )..layout();
    final textRecorder = ui.PictureRecorder();
    tp.paint(Canvas(textRecorder), Offset.zero);
    final textPic = textRecorder.endRecording();
    canvas.save();
    canvas.translate(center.dx - tp.width / 2, center.dy - tp.height / 2);
    canvas.drawPicture(textPic);
    canvas.restore();
  }

  canvas.restore();
}