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) {
// ---------------------------
// 1) Cálculo de óvalo centrado
// ---------------------------
final center = Offset(size.width / 2, size.height / 2);
// Relación de aspecto 3:4 para no deformar el óvalo
const double aspectRatio = 3.0 / 4.0;
double faceWidth = size.width * zoom;
double faceHeight = faceWidth * (1 / aspectRatio);
if (faceHeight > size.height * 0.9) {
faceHeight = size.height * 0.9;
faceWidth = faceHeight * aspectRatio;
}
final rx = faceWidth / 2; // semieje horizontal
final ry = faceHeight / 2; // semieje vertical
// Ángulo inicial (12 en punto)
final double startAngle = -math.pi / 2;
// Ángulo del progreso
final double progressAngle = startAngle + progress * 2 * math.pi;
// ---------------------------
// 2) Pintar fondo negro FUERA del óvalo
// ---------------------------
final Paint backgroundPaint = Paint()
..color = Colors.black.withValues(alpha: 0.7);
final fullRect = Rect.fromLTWH(0, 0, size.width, size.height);
final pathFull = Path()..addRect(fullRect);
final faceRect = Rect.fromCenter(
center: center,
width: faceWidth,
height: faceHeight,
);
final pathOval = Path()..addOval(faceRect);
final difference = Path.combine(
PathOperation.difference,
pathFull,
pathOval,
);
// Zona fuera del óvalo => negro semi-transparente;
// dentro => transparente.
canvas.drawPath(difference, backgroundPaint);
// ---------------------------
// 3) Dibujar listones radiales
// ---------------------------
final Paint linePaint = Paint()
..strokeWidth = strokeWidth
..strokeCap = StrokeCap.round;
for (int i = 0; i < totalLines; i++) {
final angle = startAngle + (2 * math.pi / totalLines) * i;
final dist = progressAngle - angle;
// Punto de inicio (borde interno del óvalo)
final x1 = center.dx + rx * math.cos(angle);
final y1 = center.dy + ry * math.sin(angle);
if (dist < 0) {
// Listón no alcanzado (pendiente)
linePaint.color = pendingColor;
final double lineLen = minLineLength;
final x2 = center.dx + (rx + lineLen) * math.cos(angle);
final y2 = center.dy + (ry + lineLen) * math.sin(angle);
canvas.drawLine(Offset(x1, y1), Offset(x2, y2), linePaint);
} else {
// Listón alcanzado
linePaint.color = isError ? Colors.red : reachedColor;
double lineLen;
if (dist >= decayRange) {
lineLen = minLineLength;
} else {
final t = dist / decayRange;
lineLen = maxLineLength - (maxLineLength - minLineLength) * t;
}
final x2 = center.dx + (rx + lineLen) * math.cos(angle);
final y2 = center.dy + (ry + lineLen) * math.sin(angle);
canvas.drawLine(Offset(x1, y1), Offset(x2, y2), linePaint);
}
}
// ---------------------------
// 4) Pintar texto debajo del óvalo (si message no es null/vacío)
// ---------------------------
if (message != null && message!.isNotEmpty) {
final textSpan = TextSpan(
text: message!,
style: textStyle,
);
final textPainter = TextPainter(
text: textSpan,
textAlign: TextAlign.center,
textDirection: ui.TextDirection.ltr,
);
// Layout
textPainter.layout(
minWidth: 0,
maxWidth: size.width,
);
// Coordenadas para ponerlo debajo del óvalo
// Usamos center.dy + ry para la parte baja del óvalo,
// y dejamos un margen (p.ej. 20px).
final double textX = center.dx - textPainter.width / 2;
final double textY = center.dy + ry + 20;
textPainter.paint(canvas, Offset(textX, textY));
}
}