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) {
  // Outer circle paint configuration
  Paint outerPaint = Paint()
    ..color = _color.withAlpha(150) // Change outer circle color here
    ..style = PaintingStyle.stroke
    ..strokeWidth = 4.0;

  // dot circle paint configuration
  Paint dotPaint = Paint()
    ..color = _color // Change outer circle color here
    ..style = PaintingStyle.fill
    ..strokeWidth = 2.0
    ..strokeJoin = StrokeJoin.bevel;

  // Elevation paint configuration
  Paint elevationPaint = Paint()
    ..color = _color.withAlpha(40) // Elevation color, adjust as needed 100
    ..style = PaintingStyle.stroke
    ..strokeWidth = 4.0
    ..maskFilter = const MaskFilter.blur(
        BlurStyle.normal, 4); // Blur value, adjust as needed

  double outerRadius = size.width / 6;
  Offset center = Offset(size.width / 2, size.height / 2);

  // Draw the elevation effect underneath the outer circle
  canvas.drawCircle(center + const Offset(6, 6), outerRadius, elevationPaint);

  // Draw outer circle
  canvas.drawCircle(center, outerRadius, outerPaint);

  // Calculate dot position on the path of the outer circle
  double angle = _outerCircleAnimation.value * 2 * pi;
  double x = center.dx + outerRadius * cos(angle);
  double y = center.dy + outerRadius * sin(angle);
  Offset dotCenter = Offset(x, y);

  // Draw dot on the path of outer circle
  canvas.drawCircle(dotCenter, 4.0, dotPaint);

  canvas.save();
  canvas.translate(center.dx, center.dy);

  // Inner circle paint configuration
  Paint innerPaint = Paint()
    ..color = _color // Change inner circle color here
    ..style = PaintingStyle.fill
    ..strokeWidth = 2.0;

  // Change speed of inner circle by modifying the animation value
  double innerAnimationValue = _dotAnimation.value * 4;
  canvas
      .rotate(-innerAnimationValue * pi); // Rotate in the opposite direction

  double innerRadius = size.width / 6;
  // Draw inner circle inside the outer circle
  canvas.drawCircle(Offset(innerRadius, 0.0), 4.0, innerPaint);
  canvas.restore();
}