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

  // Clip
  canvas.clipRect(Rect.fromLTWH(0, 0, size.width, size.height));

  final value = _controller.value;
  final range = _max - _min;

  if (_vertical) {
    final pxPerUnit = size.height / range;

    // Draw ticks around current value
    final firstTick =
        ((value - range / 2) / _tickInterval).floor() * _tickInterval;
    var tick = firstTick;
    while (tick <= value + range / 2) {
      final y = size.height / 2 - (tick - value) * pxPerUnit;
      if (y >= 0 && y <= size.height) {
        final isMajor = (tick % (_tickInterval * 5)).abs() < 0.001;
        final tStyle = isMajor ? _tokens.majorTick : _tokens.minorTick;
        canvas.drawLine(
          Offset(size.width - tStyle.length, y),
          Offset(size.width, y),
          Paint()
            ..color = tStyle.color
            ..strokeWidth = tStyle.strokeWidth,
        );
        if (isMajor) {
          final tp = TextPainter(
            text: TextSpan(text: _fmt(tick), style: _tokens.labelStyle),
            textDirection: TextDirection.ltr,
          )..layout();
          paintTextOnCanvas(
            canvas,
            tp,
            Offset(
                size.width - tStyle.length - tp.width - 4, y - tp.height / 2),
          );
        }
      }
      tick += _tickInterval;
    }

    // Center marker
    canvas.drawLine(
      Offset(size.width - _tokens.majorTick.length - 4, size.height / 2),
      Offset(size.width, size.height / 2),
      Paint()
        ..color = _tokens.valueColor
        ..strokeWidth = 2.5,
    );
  } else {
    final pxPerUnit = size.width / range;
    final firstTick =
        ((value - range / 2) / _tickInterval).floor() * _tickInterval;
    var tick = firstTick;
    while (tick <= value + range / 2) {
      final x = size.width / 2 + (tick - value) * pxPerUnit;
      if (x >= 0 && x <= size.width) {
        final isMajor = (tick % (_tickInterval * 5)).abs() < 0.001;
        final tStyle = isMajor ? _tokens.majorTick : _tokens.minorTick;
        canvas.drawLine(
          Offset(x, 0),
          Offset(x, tStyle.length),
          Paint()
            ..color = tStyle.color
            ..strokeWidth = tStyle.strokeWidth,
        );
        if (isMajor) {
          final tp = TextPainter(
            text: TextSpan(text: _fmt(tick), style: _tokens.labelStyle),
            textDirection: TextDirection.ltr,
          )..layout();
          paintTextOnCanvas(
              canvas, tp, Offset(x - tp.width / 2, tStyle.length + 2));
        }
      }
      tick += _tickInterval;
    }

    // Center marker
    canvas.drawLine(
      Offset(size.width / 2, 0),
      Offset(size.width / 2, _tokens.majorTick.length + 4),
      Paint()
        ..color = _tokens.valueColor
        ..strokeWidth = 2.5,
    );
  }

  // Current value badge
  if (_unit != null) {
    final label = '${_fmt(value)} $_unit';
    final tp = TextPainter(
      text: TextSpan(
        text: label,
        style: _tokens.labelStyle.copyWith(
          color: _tokens.valueColor,
          fontWeight: FontWeight.bold,
        ),
      ),
      textDirection: TextDirection.ltr,
    )..layout();
    if (_vertical) {
      paintTextOnCanvas(
          canvas, tp, Offset(4, size.height / 2 - tp.height / 2));
    } else {
      paintTextOnCanvas(canvas, tp,
          Offset(size.width / 2 - tp.width / 2, size.height - tp.height - 4));
    }
  }

  canvas.restore();
}