paint method
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:
-
Obtain an ImageStream, for example by calling ImageProvider.resolve on an AssetImage or NetworkImage object.
-
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.
-
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 double pi = 3.14;
const double circleDiameter = 32.0;
const double radius = circleDiameter / 2;
// For indeterminate state, animate the opacity of the progress circle
if (isIndeterminate) {
// Dimensions de les línies
const double lineWidth = 2.0; // Ample de la línia
const double lineLength = 6.0; // Longitud de la línia
const double lineSpacing = 4.0; // Espaiat entre les línies
// Centre del cercle
final Offset center = Offset(size.width / 2, size.height / 2);
// Calcula l'angle entre les línies
const double angleIncrement = (2 * pi) / 8;
// Dibuixa les 8 línies amb colors canviants
for (int i = 0; i < 8; i++) {
// Calcula l'angle actual de la línia
final double lineAngle = i * angleIncrement;
// Calcula el color de la línia basat en l'animació
Color lineColor = colorBackgroundSecondary1;
if (isIndeterminateAnimating) {
final double normalizedProgress = (progress * 8) % 8;
double diff = (normalizedProgress - i).abs();
if (diff > 4) diff = 8 - diff;
final int alpha = (255 * (1 - (diff / 4))).toInt().clamp(0, 255);
if (isLightTheme) {
lineColor = CDKTheme.grey700.withAlpha(alpha);
} else {
lineColor = CDKTheme.grey.withAlpha(alpha);
}
}
// Defineix el punt d'inici i final de la línia
final Offset start = center +
Offset(math.cos(lineAngle), math.sin(lineAngle)) *
(radius - lineLength - lineSpacing);
final Offset end = start +
Offset(math.cos(lineAngle), math.sin(lineAngle)) * lineLength;
// Dibuixa cada línia amb les vores arrodonides
Paint linePaint = Paint()
..color = lineColor
..strokeCap = StrokeCap
.round // Això fa que les vores de la línia siguin arrodonides
..style = PaintingStyle.stroke
..strokeWidth = lineWidth;
canvas.drawLine(start, end, linePaint);
}
} else {
Paint backgroundPaint = Paint()
..color = colorBackgroundSecondary1
..style = PaintingStyle.fill;
Paint progressPaint = Paint()
..color = hasAppFocus ? colorAccent : CDKTheme.grey
..style = PaintingStyle.fill;
// Center the circle within the canvas
final Offset center = Offset(size.width / 2, size.height / 2);
// Draw the background circle
canvas.drawCircle(center, radius, backgroundPaint);
// Calcula l'angle de barrida per al progrés determinat
final double sweepAngle = 2 * pi * progress;
// Crear un camí pel sector circular
Path path = Path()..moveTo(center.dx, center.dy);
if (progress <= 0.5) {
// Si el progrés és menor o igual al 50%, dibuixarà només un sector
path.lineTo(center.dx, center.dy - radius);
path.arcToPoint(
Offset(center.dx + radius * math.sin(sweepAngle),
center.dy - radius * math.cos(sweepAngle)),
radius: const Radius.circular(radius),
clockwise: true,
);
} else {
// Si el progrés és major que 50%, primer completa la primera meitat
path.lineTo(center.dx, center.dy - radius);
path.arcToPoint(
Offset(center.dx, center.dy + radius),
radius: const Radius.circular(radius),
clockwise: true,
);
// Després completa la segona meitat fins al progrés actual
path.arcToPoint(
Offset(center.dx + radius * math.sin(sweepAngle),
center.dy - radius * math.cos(sweepAngle)),
radius: const Radius.circular(radius),
clockwise: true,
);
}
path.close();
// Dibuixar el camí
canvas.drawPath(path, progressPaint);
}
}