stepForward method
Returns the data for the next step of this circle in the lava lamp pattern.
It uses the current data to calculate the next position of the circle.
THIS IS DESTRUCTIVE. This method will update the data of this object so next time it can be used to get the next position of the circle.
Implementation
void stepForward(double animationValue) {
final double rawPhase = animationValue + _fadePhase;
animationValue = rawPhase == 1.0 ? 1.0 : rawPhase % 1.0;
if (animationValue < _previousAnimationValue) {
_previousAnimationValue = 0.0;
_previousStep = -1;
}
// Calculate the next width of the circle.
if (growAndShrink) {
const double growthRateMultiplier = 0.01;
if (_isGrowing) {
width += growthRate * growthRateMultiplier;
if (width >= _originalWidth + widthTolerance) {
_isGrowing = false;
width = _originalWidth + widthTolerance;
}
} else {
width -= growthRate * growthRateMultiplier;
if (width <= _originalWidth - widthTolerance) {
_isGrowing = true;
width = _originalWidth - widthTolerance;
}
}
}
// Calculate the next color of the circle.
if (fadeBetweenColors) {
final double stepLength = 1.0 / colors.length;
final int step =
min((animationValue / stepLength).floor(), colors.length - 1);
if (_previousStep < step || _previousStep < 0) {
_previousStep = step;
_colorIndex = __nextColorIndex;
_setNextColor();
}
color = Color.lerp(
colors[_colorIndex],
colors[__nextColorIndex],
(animationValue - (step * stepLength)) / stepLength,
) ??
colors[_colorIndex];
}
// Calculate the next position of the circle.
const double speedMultiplier = 0.075;
final double radians = direction.degrees * (pi / 180);
final double x = position.dx + ((speed * speedMultiplier) * cos(radians));
final double y = position.dy + ((speed * speedMultiplier) * sin(radians));
position = Offset(x, y);
_previousAnimationValue = animationValue;
}