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) {
final center = Offset(size.width / 2, size.height / 2);
final progress = isRefreshing ? 1.0 : pullProgress.clamp(0.0, 1.0);
// Rocket vertical offset (translates upwards during active refreshing)
final launchY = isRefreshing ? -refreshProgress * size.height * 1.2 : 0.0;
final rCenter = Offset(
center.dx, center.dy + size.height * 0.2 * (1.0 - progress) + launchY);
// 1. Draw Engine Flames & exhaust particles
final flamePaint = Paint()..style = PaintingStyle.fill;
final rand = math.Random(1111);
if (progress > 0.1) {
final activeFlames = (particleCount * progress * flameIntensity).round();
for (int i = 0; i < activeFlames; i++) {
final t =
(rand.nextDouble() + (isRefreshing ? refreshProgress : 0.0)) % 1.0;
final flameX = rCenter.dx + (rand.nextDouble() - 0.5) * 12.0 * progress;
// Flames shoot downwards
final flameY = rCenter.dy + 15 + t * 30.0 * progress;
flamePaint.color = Color.lerp(Colors.redAccent, Colors.orangeAccent, t)!
.withValues(alpha: (1.0 - t) * 0.8 * progress);
canvas.drawCircle(
Offset(flameX, flameY), (1.0 - t) * 5.0 * progress, flamePaint);
}
}
// 2. Draw Rocket Hull Outline
final rocketPaint = Paint()
..color = color
..strokeWidth = 2.0
..style = PaintingStyle.stroke;
final fillPaint = Paint()
..color = Colors.black.withValues(alpha: 0.15)
..style = PaintingStyle.fill;
final s = rocketSize / 2;
final rPath = Path()
// Nose cone
..moveTo(rCenter.dx, rCenter.dy - s)
..quadraticBezierTo(rCenter.dx + s * 0.4, rCenter.dy - s * 0.3,
rCenter.dx + s * 0.4, rCenter.dy + s * 0.4)
// Right wing fin
..lineTo(rCenter.dx + s * 0.85, rCenter.dy + s * 0.8)
..lineTo(rCenter.dx + s * 0.4, rCenter.dy + s * 0.6)
// Base engine exhaust bell
..lineTo(rCenter.dx + s * 0.2, rCenter.dy + s * 0.6)
..lineTo(rCenter.dx + s * 0.15, rCenter.dy + s * 0.75)
..lineTo(rCenter.dx - s * 0.15, rCenter.dy + s * 0.75)
..lineTo(rCenter.dx - s * 0.2, rCenter.dy + s * 0.6)
// Left wing fin
..lineTo(rCenter.dx - s * 0.4, rCenter.dy + s * 0.6)
..lineTo(rCenter.dx - s * 0.85, rCenter.dy + s * 0.8)
..lineTo(rCenter.dx - s * 0.4, rCenter.dy + s * 0.4)
..quadraticBezierTo(rCenter.dx - s * 0.4, rCenter.dy - s * 0.3,
rCenter.dx, rCenter.dy - s)
..close();
canvas.drawPath(rPath, fillPaint);
canvas.drawPath(rPath, rocketPaint);
// Rocket window
canvas.drawCircle(Offset(rCenter.dx, rCenter.dy), s * 0.2, rocketPaint);
}