Skip to content

Instantly share code, notes, and snippets.

@av
Created October 25, 2019 13:46
Show Gist options
  • Save av/f27a4bed6c05bf04dcc3c370da8eefd6 to your computer and use it in GitHub Desktop.
Save av/f27a4bed6c05bf04dcc3c370da8eefd6 to your computer and use it in GitHub Desktop.
Flutter: magic, particle Generator
/// A function which returns [Particle] when called
typedef ParticleProvider = Particle Function(int i);
/// A [CompositeParticle] which allows to use [ParticleProvider]
/// generator functions as source for children particles
///
/// ```dart
/// // 10 plain fading circles
/// ParticleGenerator(10, (i) => FadingCircle());
///
/// // 10 randomly sized fading circles
/// ParticleGenerator(10, (i) => FadingCircle(radius: Randoms.rnd.nextDouble() * 10));
///
/// // 5 small and 5 large fading circles
/// ParticleGenerator(10, (i) => FadingCircle(radius: i < 5 ? 10 : 20));
/// ```
class ParticleGenerator extends Particle with CompositeParticle {
List<Particle> children;
ParticleGenerator(
int count,
ParticleProvider generator,
) {
this.children = List<Particle>.generate(count, generator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment