paint method

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

Paints the dashed circle on the canvas.

This method calculates the optimal number of dashes to fit evenly around the circle and draws them with rounded caps for a smooth appearance.

Implementation

@override
void paint(Canvas canvas, Size size) {
  final paint = Paint()
    ..color = color
    ..strokeWidth = strokeWidth
    ..style = PaintingStyle.stroke
    ..strokeCap = StrokeCap.round;

  final center = Offset(size.width / 2, size.height / 2);
  final radius = (size.width - strokeWidth) / 2;

  // Calculate the total circumference
  final circumference = 2 * math.pi * radius;

  // Calculate how many dashes fit around the circle
  final dashSpaceLength = dashLength + dashGap;
  final dashCount = (circumference / dashSpaceLength).floor();

  // Adjust dash parameters to fit perfectly around the circle
  final adjustedDashSpaceLength = circumference / dashCount;
  final adjustedDashLength = dashLength * (adjustedDashSpaceLength / dashSpaceLength);

  // Draw the dashed circle
  for (int i = 0; i < dashCount; i++) {
    final startAngle = (i * adjustedDashSpaceLength / radius) - (math.pi / 2);
    final sweepAngle = adjustedDashLength / radius;

    final path = Path();
    path.addArc(
      Rect.fromCircle(center: center, radius: radius),
      startAngle,
      sweepAngle,
    );

    canvas.drawPath(path, paint);
  }
}