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 t = progress * math.pi * 2;
final center = Offset(size.width / 2, size.height / 2);
// 1. Draw solid glowing core
final corePaint = Paint()
..shader = RadialGradient(
colors: [
const Color(0xFFFFFFFF),
const Color(0xFFF3EDFF),
],
stops: const [0.3, 1.0],
).createShader(Rect.fromCircle(center: center, radius: size.width / 2));
canvas.drawCircle(center, size.width / 2, corePaint);
// 2. Helper to draw individual waves
void drawWave(
Color color,
double width,
double height,
double baseRotation,
Offset offset,
double opacity,
) {
canvas.save();
// Translate to overall center + the offset
canvas.translate(center.dx + offset.dx, center.dy + offset.dy);
// Rotate
canvas.rotate(baseRotation);
final squashWobble = math.sin(t * 3.0 + baseRotation) * 20;
final stretchedWidth =
math.max(1.0, width + squashWobble + (amplitude * 120)) / 2;
final stretchedHeight =
math.max(1.0, height - squashWobble + (amplitude * 80)) / 2;
final paint = Paint()
..shader = RadialGradient(
colors: [
color.withValues(alpha: opacity),
color.withValues(alpha: 0.0),
],
stops: const [0.2, 1.0],
).createShader(Rect.fromCenter(
center: Offset.zero,
width: stretchedWidth * 2,
height: stretchedHeight * 2,
));
// Draw an oval stretching around this wave's offset
canvas.drawOval(
Rect.fromCenter(
center: Offset.zero,
width: stretchedWidth * 2,
height: stretchedHeight * 2,
),
paint,
);
canvas.restore();
}
// Safe color access helper
Color getColor(int index) => colors[index % colors.length];
// Wave 1: Broad swath
drawWave(
getColor(0),
320,
160,
math.sin(t * 1.0) * 0.2,
Offset(math.cos(t * 2.0) * 10, math.sin(t * 1.0) * 40),
0.2,
);
// Wave 3: Light edge
drawWave(
getColor(1),
300 * scaleFactor,
150 * scaleFactor,
math.sin(t * 2.0) * -0.2 + 0.1,
Offset(math.sin(t * 3.0) * 30 * scaleFactor,
math.cos(t * 1.0) * 45 * scaleFactor),
1.0,
);
// Wave 4: Internal complexity
drawWave(
getColor(2),
360,
140,
math.cos(t * 1.0) * 0.25,
Offset(math.cos(t * 2.0) * -15, math.sin(t * 2.0) * -45),
1.0,
);
// Wave 5: Prominent frame
drawWave(
getColor(3),
300 * scaleFactor,
180 * scaleFactor,
math.sin(t * 2.0) * 0.15 - 0.1,
Offset(math.sin(t * 1.0) * 35 * scaleFactor,
math.cos(t * 2.0) * -50 * scaleFactor),
1.0,
);
// Wave 6: Sharp white highlight swirls slicing across edges
drawWave(
Colors.white,
280 * scaleFactor,
35 * scaleFactor,
math.cos(t * 2.0) * 0.15 + 0.2,
Offset(math.sin(t * 2.0) * -15 * scaleFactor,
math.sin(t * 2.0 + math.pi / 2) * 55 * scaleFactor),
0.9,
);
// Wave 7: Secondary bright highlight to give glossy 3D depth
drawWave(
Colors.white,
200 * scaleFactor,
15 * scaleFactor,
math.sin(t * 1.0) * -0.2 - 0.1,
Offset(math.cos(t * 2.0) * 20 * scaleFactor,
math.cos(t * 1.0 + math.pi) * 40 * scaleFactor),
0.75,
);
}