MagneticSim class

Simulates a magnetic attraction force toward a target point.

The pull increases as the object approaches the target, producing a smooth snap-to behavior without oscillation.

Useful for: snap targets, docking, magnetic buttons, pull-to-refresh snap.

Usage:

final sim = MagneticSim(position0: 0.0, target: 1.0, strength: 50.0);
final pos = sim.position(0.5);
Inheritance

Constructors

MagneticSim({required double position0, required double target, double strength = 50.0, double restThreshold = 0.01})
const

Properties

hashCode int
The hash code for this object.
no setterinherited
id String
Unique identifier for this simulator type.
no setteroverride
position0 double
Starting position.
final
restThreshold double
Rest threshold — how close to target counts as "done".
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
strength double
Strength of the magnetic field (higher = snaps faster).
final
target double
Target (attraction point).
final

Methods

force(double t) double
Attraction force magnitude at time t.
isDone(double t) bool
True when the simulation has effectively come to rest.
override
normalized(double t, {required double from, required double to}) double
Normalizes position to 0.0, 1.0 based on from and to. Useful for driving AnimationController.value.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
position(double t) double
Position at time t (seconds).
override
sample({double durationSec = 2.0, int sampleHz = 60}) List<double>
Samples the simulation at sampleHz for durationSec seconds.
inherited
settleDuration({double maxTime = 10.0, int resolution = 120}) double
Returns the time (seconds) at which isDone first becomes true, sampled at resolution steps per second. Returns maxTime if never done.
inherited
toString() String
A string representation of this object.
override
velocity(double t) double
Velocity at time t (units/second).
override

Operators

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

Static Methods

dock({required double position0, required double target}) MagneticSim
Dock behavior — used for element snapping to grid/anchor points.
gentle({double position0 = 0.0, double target = 1.0}) MagneticSim
Gentle pull — soft, gradual attraction.
snap({double position0 = 0.0, double target = 1.0}) MagneticSim
Quick snap — strong field, fast locking.