onPaint method

  1. @override
void onPaint(
  1. Canvas canvas
)
override

Draws segment in series bounds.

Implementation

@override
void onPaint(Canvas canvas) {
  final Paint strokePaint = getStrokePaint();
  if (points.isNotEmpty &&
      points.length == 8 &&
      strokePaint.strokeWidth > 0 &&
      strokePaint.color != Colors.transparent) {
    final Offset? maxStart =
        Offset.lerp(_oldPoints[0], points[0], animationFactor);
    final Offset? maxEnd =
        Offset.lerp(_oldPoints[1], points[1], animationFactor);
    if (maxStart != null && maxEnd != null) {
      drawDashes(canvas, series.dashArray, strokePaint,
          start: maxStart, end: maxEnd);
    }

    final Offset? maxConnectorStart =
        Offset.lerp(_oldPoints[2], points[2], animationFactor);
    final Offset? maxConnectorEnd =
        Offset.lerp(_oldPoints[3], points[3], animationFactor);
    if (maxConnectorStart != null && maxConnectorEnd != null) {
      drawDashes(canvas, series.dashArray, strokePaint,
          start: maxConnectorStart, end: maxConnectorEnd);
    }

    final Offset? minStart =
        Offset.lerp(_oldPoints[4], points[4], animationFactor);
    final Offset? minEnd =
        Offset.lerp(_oldPoints[5], points[5], animationFactor);
    if (minStart != null && minEnd != null) {
      drawDashes(canvas, series.dashArray, strokePaint,
          start: minStart, end: minEnd);
    }

    final Offset? minConnectorStart =
        Offset.lerp(_oldPoints[6], points[6], animationFactor);
    final Offset? minConnectorEnd =
        Offset.lerp(_oldPoints[7], points[7], animationFactor);
    if (minConnectorStart != null && minConnectorEnd != null) {
      drawDashes(canvas, series.dashArray, strokePaint,
          start: minConnectorStart, end: minConnectorEnd);
    }
  }

  if (segmentRect == null) {
    return;
  }

  final Rect? paintRect =
      Rect.lerp(_oldSegmentRect, segmentRect, animationFactor);
  if (paintRect == null) {
    return;
  }

  final Paint fillPaint = getFillPaint();
  if (fillPaint.color != Colors.transparent && !paintRect.isEmpty) {
    canvas.drawRect(paintRect, fillPaint);
  }

  final double strokeWidth = strokePaint.strokeWidth;
  if (strokeWidth > 0 && strokePaint.color != Colors.transparent) {
    final Path strokePath = strokePathFromRRect(
        RRect.fromRectAndRadius(paintRect, Radius.zero), strokeWidth);
    drawDashes(canvas, series.dashArray, strokePaint, path: strokePath);
  }

  if (_medianLinePoints.isNotEmpty) {
    drawDashes(canvas, series.dashArray, strokePaint,
        start: _medianLinePoints[0], end: _medianLinePoints[1]);
  }

  if (animationFactor > 0.75 && _meanLinePoints.isNotEmpty) {
    // Mean animation starts at 0.75 and ends at 1.0. So, the animation falls
    // only 0.25. So, 0.25 * 4 = 1.0. So, the animation factor is multiplied
    // by 4 to get the animation factor for mean line.
    final double opacity = (animationFactor - 0.75) * 4.0;
    final Paint meanPaint = Paint()
      ..color = strokePaint.color.withOpacity(opacity)
      ..strokeWidth = strokePaint.strokeWidth
      ..shader = strokePaint.shader
      ..style = strokePaint.style
      ..strokeCap = strokePaint.strokeCap
      ..strokeJoin = strokePaint.strokeJoin
      ..strokeMiterLimit = strokePaint.strokeMiterLimit;
    drawDashes(canvas, series.dashArray, meanPaint,
        start: _meanLinePoints[0], end: _meanLinePoints[1]);
    drawDashes(canvas, series.dashArray, meanPaint,
        start: _meanLinePoints[2], end: _meanLinePoints[3]);
  }
}