createParticle method
Creates a new particle within specified bounds
size - The available space where particles can be placed
Returns: A new Particle instance with randomized properties
Implementation
@override
Particle createParticle(Size size) {
// Generate random velocity vector components:
// (random.nextDouble() - 0.5) creates values in [-0.5, 0.5]
// Multiplying by 2*maxSpeed gives range [-maxSpeed, maxSpeed]
final Offset velocity = Offset(
(random.nextDouble() - 0.5) * maxSpeed,
(random.nextDouble() - 0.5) * maxSpeed,
);
// Create and return new particle with randomized properties
return Particle(
color: color,
// Random position within bounds (0 to size.width/height)
position: Offset(
random.nextDouble() * size.width,
random.nextDouble() * size.height,
),
velocity: velocity,
// Random size between 1 and maxSize (avoiding 0-sized particles)
size: random.nextDouble() * maxSize + 1,
);
}