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) {
  const startAngle = -90;
  final r = size.height / 2 - thickness! / 2;
  const startAngleRad = startAngle * (math.pi / 180.0);
  final firstArcCenter = Offset(
      size.width / 2 + ((size.width / 2 - r) - thickness! / 2),
      size.height / 2);
  final startPointX = size.width / 2;
  final firstArcStartX = size.width - r - thickness! / 2;
  final secondLineEndX = startPointX;
  final thirdLineEndX = r + thickness! / 2;
  const piInRadian = 180 * (math.pi / 180.0);
  final secondArcCenter = Offset(r + thickness! / 2, size.height / 2);

  var progress = progressValue * 6;
  int x = (progress / 100).floor();
  var sections = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
  for (int i = 0; i < x.toInt(); i++) {
    sections[i] = 1;
  }
  if (x.toInt() <= 5) {
    sections[x.toInt()] = (progress % 100) / 100;
  }

  final len1 = firstArcStartX - startPointX;
  final len2 = firstArcStartX - secondLineEndX;
  final len3 = secondLineEndX - thirdLineEndX;
  final len4 = startPointX - thirdLineEndX;

  final paint = Paint()
    ..color = color
    ..strokeCap = StrokeCap.butt
    ..style = PaintingStyle.stroke
    ..strokeWidth = width;

  drawFirstFullPath() {
    final fullFirstPath = Path()
      ..moveTo(startPointX, thickness! / 2)
      ..lineTo(firstArcStartX, thickness! / 2);
    animateCoderPath(fullFirstPath, paint, canvas, firstAnimatedProgress);
  }

  drawSecondFullPath() {
    final secondPath = Path()
      ..arcTo(Rect.fromCircle(center: firstArcCenter, radius: r),
          startAngleRad, sections[1] * (180) * (math.pi / 180.0), true);
    animateCoderPath(secondPath, paint, canvas, secondAnimatedProgress);
  }

  drawThirdFullPath() {
    final thirdPath = Path()
          ..moveTo(
              size.width - r - thickness! / 2, size.height - thickness! / 2)
          ..lineTo(firstArcStartX - len2 * sections[2],
              size.height - thickness! / 2) //line2
        ;
    animateCoderPath(thirdPath, paint, canvas, thirdAnimatedProgress);
  }

  drawFourthFullPath() {
    final fourthPath = Path()
          ..moveTo(size.width / 2, size.height - thickness! / 2)
          ..lineTo(secondLineEndX - len3 * sections[3],
              size.height - thickness! / 2) //line3
        ;
    animateCoderPath(fourthPath, paint, canvas, fourthAnimatedProgress);
  }

  drawFifthFullPath() {
    final fifthPath = Path()
      ..arcTo(
          Rect.fromCircle(center: secondArcCenter, radius: r),
          startAngleRad + piInRadian,
          sections[4] * (180) * (math.pi / 180.0),
          true);
    animateCoderPath(fifthPath, paint, canvas, fifthAnimatedProgress);
  }

  if (x == 0) {
    final path = Path()
          ..moveTo(startPointX, thickness! / 2)
          ..lineTo(startPointX + len1 * sections[0], thickness! / 2) //line1
        ;
    animateCoderPath(path, paint, canvas, firstAnimatedProgress);
  } else if (x == 1) {
    drawFirstFullPath();
    final secondPath = Path()
      ..arcTo(Rect.fromCircle(center: firstArcCenter, radius: r),
          startAngleRad, sections[1] * (180) * (math.pi / 180.0), true);
    animateCoderPath(secondPath, paint, canvas, secondAnimatedProgress);
  } else if (x == 2) {
    drawFirstFullPath();
    drawSecondFullPath();
    final thirdPath = Path()
          ..moveTo(
              size.width - r - thickness! / 2, size.height - thickness! / 2)
          ..lineTo(firstArcStartX - len2 * sections[2],
              size.height - thickness! / 2) //line2
        ;
    animateCoderPath(thirdPath, paint, canvas, secondAnimatedProgress);
  } else if (x == 3) {
    drawFirstFullPath();
    drawSecondFullPath();
    drawThirdFullPath();
    final fourthPath = Path()
          ..moveTo(size.width / 2, size.height - thickness! / 2)
          ..lineTo(secondLineEndX - len3 * sections[3],
              size.height - thickness! / 2) //line3
        ;
    animateCoderPath(fourthPath, paint, canvas, secondAnimatedProgress);
  } else if (x == 4) {
    drawFirstFullPath();
    drawSecondFullPath();
    drawThirdFullPath();
    drawFourthFullPath();

    final fifthPath = Path()
      ..arcTo(
          Rect.fromCircle(center: secondArcCenter, radius: r),
          startAngleRad + piInRadian,
          sections[4] * (180) * (math.pi / 180.0),
          true);
    animateCoderPath(fifthPath, paint, canvas, fifthAnimatedProgress);
  } else {
    drawFirstFullPath();
    drawSecondFullPath();
    drawThirdFullPath();
    drawFourthFullPath();
    drawFifthFullPath();
    final sixthPath = Path()
      ..moveTo(r + thickness! / 2, thickness! / 2)
      ..lineTo(thirdLineEndX + len4 * sections[5], thickness! / 2); //line4
    animateCoderPath(sixthPath, paint, canvas, sixthAnimatedProgress);
  }
}