Sub topic 1
How Animation Works in Flutter
Flutter animation is the gradual change of a value over time. The value might represent a width, color, position, opacity, rotation, or another visual property.
Flutter draws the screen in frames. On a typical device, it tries to produce about 60 frames per second. During an animation, Flutter calculates a new value for each frame and rebuilds the widgets that depend on that value. The eye sees these small changes as continuous motion.
For example, a box can change its width from 100 to 200. Flutter does not jump directly between the two values. It produces intermediate values such as 120, 145, and 180, depending on the elapsed time and the chosen motion curve.
Prerequisites
This chapter assumes that you know basic Dart syntax, Flutter widgets, build, StatelessWidget, StatefulWidget, and setState. You should also understand basic layout widgets such as Container, Row, and Column.
The animation pipeline
Most Flutter animations follow this sequence:
- An animation starts with an initial value.
- Time passes while Flutter schedules frames.
- A value changes for each frame.
- A widget uses the current value in its
buildmethod. - Flutter paints the updated result.
The animation value is often normalized between 0.0 and 1.0. A value of 0.0 means the animation has not started, and 1.0 means it has reached its end. The application can map that value to a useful range, such as a width from 100 to 200.
A frame is one complete image produced for the screen. A rebuild means Flutter calls a widget's build method again. Rebuilding does not necessarily mean that every object is recreated or that the whole screen is repainted. Flutter compares the new widget tree with the previous one and updates the parts that changed.
double widthFor(double progress) {
const start = 100.0;
const end = 200.0;
return start + (end - start) * progress;
}
void main() {
for (final progress in [0.0, 0.5, 1.0]) {
print(widthFor(progress));
}
}This function maps progress values to widths. Animation APIs provide the changing progress value; your widget uses it to calculate visual properties.
An animation can also use a curve. A curve changes how quickly progress advances. Linear motion advances evenly. An ease-out motion moves quickly at first and slows near the end. The duration controls how long the complete change takes.
Choosing what changes
Start by identifying the property that should vary. If the requirement is simply “animate this property when it changes,” an implicit animation is usually the simplest choice. If the requirement needs playback commands, synchronization, or several timed phases, an explicit animation gives more control. Those approaches use the same basic model: calculate changing values and apply them during frame updates.
Exercise
Why does Flutter need to rebuild a widget during an animation?
Show answer
The widget must receive the current frame value so it can produce the updated visual property for that frame.