paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Size size
)
override

Called whenever the object needs to paint. The given Canvas has its coordinate space configured such that the origin is at the top left of the box. The area of the box is the size of the size argument.

Paint operations should remain inside the given area. Graphical operations outside the bounds may be silently ignored, clipped, or not clipped. It may sometimes be difficult to guarantee that a certain operation is inside the bounds (e.g., drawing a rectangle whose size is determined by user inputs). In that case, consider calling Canvas.clipRect at the beginning of paint so everything that follows will be guaranteed to only draw within the clipped area.

Implementations should be wary of correctly pairing any calls to Canvas.save/Canvas.saveLayer and Canvas.restore, otherwise all subsequent painting on this canvas may be affected, with potentially hilarious but confusing results.

To paint text on a Canvas, use a TextPainter.

To paint an image on a Canvas:

  1. Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.

  2. Whenever the ImageStream's underlying ImageInfo object changes (see ImageStream.addListener), create a new instance of your custom paint delegate, giving it the new ImageInfo object.

  3. In your delegate's paint method, call the Canvas.drawImage, Canvas.drawImageRect, or Canvas.drawImageNine methods to paint the ImageInfo.image object, applying the ImageInfo.scale value to obtain the correct rendering size.

Implementation

@override
void paint(Canvas canvas, Size size) {
  canvas = super.paintOrDebug(canvas, size);

  if (canPaint) {
    //[1] Calculate and search for upperBound of total path length which should be painted
    double upperBound = this.animation.value * totalPathSum;
    int currentIndex = this.paintedSegmentIndex;
    double currentLength = this._paintedLength;
    while (currentIndex < pathSegments.length - 1) {
      if (currentLength + pathSegments[currentIndex].length < upperBound) {
        toPaint.add(pathSegments[currentIndex]);
        currentLength += pathSegments[currentIndex].length;
        currentIndex++;
      } else {
        break;
      }
    }
    //[2] Extract subPath of last path which breaks the upperBound
    double subPathLength = upperBound - currentLength;
    PathSegment lastPathSegment = pathSegments[currentIndex];

    Path subPath = lastPathSegment.path
        .computeMetrics()
        .first
        .extractPath(0, subPathLength);
    this.paintedSegmentIndex = currentIndex;
    this._paintedLength = currentLength;
    // //[3] Paint all selected paths to canvas
    late Paint paint;
    late Path tmp;
    if (this.animation.value == 1.0) {
      //hotfix: to ensure callback for last segment TODO not pretty
      toPaint.clear();
      toPaint.addAll(pathSegments);
    } else {
      //[3.1] Add last subPath temporarily
      tmp = Path.from(lastPathSegment.path);
      lastPathSegment.path = subPath;
      toPaint.add(lastPathSegment);
    }
    //[3.2] Restore rendering order - last path element in original PathOrder should be last painted -> most visible
    //[3.3] Paint elements
    (toPaint..sort(Extractor.getComparator(PathOrders.original)))
        .forEach((segment) {
      paint = (this.paints.isNotEmpty)
          ? this.paints[segment.pathIndex]
          : (new Paint() //Paint per path TODO implement Paint per PathSegment?
            //TODO Debug disappearing first lineSegment
            // ..color = (segment.relativeIndex == 0 && segment.pathIndex== 0) ? Colors.red : ((segment.relativeIndex == 1) ? Colors.blue : segment.color)
            ..color = segment.color
            ..style = PaintingStyle.stroke
            ..strokeCap = StrokeCap.butt
            ..strokeWidth = segment.strokeWidth);
      paint.strokeCap = StrokeCap.butt;
      paint.strokeWidth = 2;
      canvas.drawPath(segment.path, paint);
    });

    if (this.animation.value != 1.0) {
      //[3.4] Remove last subPath
      toPaint.remove(lastPathSegment);
      lastPathSegment.path = tmp;
    }

    //TODO Problem: Path drawning is a continous iteration over the length of all segments. To make a callback which fires exactly when path is drawn is therefore not possible (I can only ensure one of the two cases: 1) segment is completely drawn 2) no next segment was started to be drawn yet - For now: 1)
    // double remainingLength = lastPathSegment.length - subPathLength;

    super.onFinish(canvas, size, lastPainted: toPaint.length - 1);
  } else {
    this.paintedSegmentIndex = 0;
    this._paintedLength = 0.0;
    this.toPaint.clear();
  }
}