ParticleField class

ParticleField lets you add custom particle effects anywhere in your Flutter application easily. Simply provide a SpriteSheet (or ImageFrameProvider) and an onTick handler that manages your Particle list.

ParticleField field = ParticleField(
  spriteSheet: sparkleSpriteSheet,
  // top left will be 0,0:
  origin: Alignment.topLeft,
  // onTick is where all the magic happens:
  onTick: (controller, elapsed, size) {
    List<Particle> particles = controller.particles;
    // add a new particle each frame:
    particles.add(Particle(x: rnd(size.width), vx: rnd(-1, 1)));
    // update existing particles:
    for (int i = particles.length - 1; i >= 0; i--) {
      Particle particle = particles[i];
      // call update, which automatically adds vx/vy to x/y
      // add some gravity (ie. increase vertical velocity)
      // and increment the frame
      particle.update(vy: particle.vy + 0.1, frame: particle.frame + 1);
      // remove particle if it's out of bounds:
      if (!size.contains(particle.toOffset())) particles.removeAt(i);
    }
  },
)
Inheritance

Constructors

ParticleField({required ImageFrameProvider spriteSheet, required ParticleFieldTick onTick, ParticleFieldInit? onInit, BlendMode blendMode = BlendMode.srcIn, Alignment origin = Alignment.center, Alignment anchor = Alignment.center, Key? key})
const

Properties

anchor Alignment
Specifies the anchor point (ie. where x=0, y=0) for the particles. This is the point around which the particle rotates, and from which it is drawn.
final
blendMode BlendMode
The BlendMode to use when compositing Particle.color with the particle frame image. Defaults to BlendMode.srcIn.
final
hashCode int
The hash code for this object.
no setterinherited
key Key?
Controls how one widget replaces another widget in the tree.
finalinherited
onInit ParticleFieldInit?
Called when the ParticleController first initializes. This provides an opportunity to perform setup functions, like pre-populating the particle list with initial particles.
final
onTick ParticleFieldTick
Called each frame immediately before _ParticleFieldPainter renders the particles. This is where logic to add, remove, and update your particles will normally live.
final
origin Alignment
Specifies the origin point (ie. where x=0, y=0) of the particle field's coordinate system. For example, Alignment.center would position the origin in the middle of the field, Alignment.topLeft would set the origin at the top left.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
spriteSheet ImageFrameProvider
Provides the image frames for this particle system. See SpriteSheet.
final

Methods

createElement() StatefulElement
Creates a StatefulElement to manage this widget's location in the tree.
inherited
createState() State<ParticleField>
Creates the mutable state for this widget at a given location in the tree.
override
debugDescribeChildren() List<DiagnosticsNode>
Returns a list of DiagnosticsNode objects describing this node's children.
inherited
debugFillProperties(DiagnosticPropertiesBuilder properties) → void
Add additional properties associated with the node.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
stackAbove({double scale = 1.0, required Widget child}) Widget
A convenience function that layers this ParticleField in front of the specified child, sizes it to match, and optionally scales it (this can be useful for providing an "overscan" region).
stackBelow({double scale = 1.0, required Widget child}) Widget
A convenience function that layers this ParticleField behind the specified child, sizes it to match, and optionally scales it (this can be useful for providing an "overscan" region).
toDiagnosticsNode({String? name, DiagnosticsTreeStyle? style}) DiagnosticsNode
Returns a debug representation of the object that is used by debugging tools and by DiagnosticsNode.toStringDeep.
inherited
toString({DiagnosticLevel minLevel = DiagnosticLevel.info}) String
A string representation of this object.
inherited
toStringDeep({String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a string representation of this node and its descendants.
inherited
toStringShallow({String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) String
Returns a one-line detailed description of the object.
inherited
toStringShort() String
A short, textual description of this widget.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited