Sub topic 7
When implicit animations are not enough
This chapter assumes you can write a StatefulWidget, call setState, and use AnimatedContainer and TweenAnimationBuilder. It covers where those widgets stop being enough and which signs tell you to switch to an explicit animation.
What implicit animations promise
An implicit animation widget has a narrow contract. You give it a target value, and when the target changes, it animates from the current value to the new one. The widget owns the controller, the ticker, and the disposal.
That contract is why implicit animations are short to write. It is also their limit. You describe where the value should end up, not how time passes. Anything that needs control over time, such as starting, stopping, reversing, repeating, or reading the progress, falls outside the contract.
The limits
Nothing happens without a change
An implicit animation only starts when a property differs from the previous build. AnimatedContainer does not animate on its first build, because there is no previous value. You can't ask an implicit widget to "play now".
TweenAnimationBuilder is a partial exception. It animates from begin to end on its first build. After that it is back to the same rule: it animates only when end changes.
No looping, no reversing on demand
Implicit widgets have no repeat() or reverse() method. You can only change the target. The usual workaround is onEnd, which flips a flag so the target changes again:
import 'package:flutter/material.dart';
class PulseHack extends StatefulWidget {
const PulseHack({super.key});
@override
State<PulseHack> createState() => _PulseHackState();
}
class _PulseHackState extends State<PulseHack> {
bool _grown = false;
@override
Widget build(BuildContext context) {
return TweenAnimationBuilder<double>(
tween: Tween<double>(begin: 1.0, end: _grown ? 1.2 : 0.8),
duration: const Duration(seconds: 1),
curve: Curves.easeInOut,
onEnd: () => setState(() => _grown = !_grown),
builder: (context, scale, child) {
return Transform.scale(scale: scale, child: child);
},
child: const FlutterLogo(size: 80),
);
}
}This works, but it is fragile.
- Every loop iteration goes through
setStateand rebuilds the wholePulseHackwidget. - There is no way to stop the loop except by adding more state.
- The first leg starts from 1.0 and moves toward 0.8, so the pulse range is not symmetrical on the first cycle.
- If the widget is rebuilt for another reason mid-animation, the flag and the visible motion can disagree.
An AnimationController with repeat(reverse: true) does the same job with no state flag and no rebuilds of the parent.
No access to progress
TweenAnimationBuilder hands you the current value, but not a progress number from 0 to 1. You also can't read the animation status (forward, reverse, completed, dismissed). The only lifecycle hook is onEnd, which fires when the animation reaches its target.
No scrubbing or gesture control
If the animation position must follow a drag, a slider, or a scroll offset, you need to set the position directly. Implicit widgets don't expose a position you can set. With a controller you assign controller.value:
import 'package:flutter/material.dart';
class ScrubbableBox extends StatefulWidget {
const ScrubbableBox({super.key});
@override
State<ScrubbableBox> createState() => _ScrubbableBoxState();
}
class _ScrubbableBoxState extends State<ScrubbableBox>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
late final Animation<double> _offset =
Tween<double>(begin: 0, end: 200).animate(_controller);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _controller,
builder: (context, _) {
return Column(
children: [
SizedBox(
height: 60,
width: 260,
child: Stack(
children: [
Positioned(
left: _offset.value,
child: Container(
width: 50,
height: 50,
color: Colors.teal,
),
),
],
),
),
Slider(
value: _controller.value,
onChanged: (v) => _controller.value = v,
),
],
);
},
);
}
}Dragging the slider moves the box to the matching position. No implicit widget can do this, because none of them lets you choose the animation's progress.
No shared timeline
Each implicit widget runs its own private controller. Two implicit widgets that change at the same moment start together, but they can't share one timeline. You can't make one effect run during the first half of a duration and another during the last 60%. That needs one controller and several Interval curves, which a later chapter covers.
No custom motion models
Implicit widgets interpolate between two values with a curve. If you want a spring, a fling with velocity, or a simulation, you need AnimationController methods such as animateWith.
Summary of capabilities
| Requirement | Implicit widgets | Explicit animation |
|---|---|---|
| Animate when a property changes | Yes | Yes |
| Play on first build | Only `TweenAnimationBuilder` | Yes, call `forward()` |
| Loop or ping-pong | Workaround with `onEnd` | `repeat(reverse: true)` |
| Pause, stop, reverse mid-flight | No | `stop()`, `reverse()` |
| Read progress or status | No | `value`, `status`, listeners |
| Follow a gesture | No | Set `controller.value` |
| Coordinate several effects | No | One controller, several animations |
| Physics-based motion | No | `animateWith` |
Recognizing the switch point
A single yes sends you to the explicit side. If every answer is no, stay with the implicit widget. It has less code and no disposal to forget.
Common mistakes when stretching implicit widgets
- Building loops from
onEnd. It works for a simple pulse. It becomes a bug source when you need to stop or restart the loop. - Calling
setStateon a timer to fake animation. ATimer.periodicthat updates a value 60 times a second skips the frame scheduler, so it can drop frames or run while the app is in the background. A controller is tied to the display's frame callbacks. - Expecting
AnimatedContainerto animate on mount. It won't. Either trigger a state change after the first frame, or useTweenAnimationBuilder, or go explicit. - Changing the tween's
beginand expecting a restart.TweenAnimationBuilderonly usesbeginthe first time. After that, it animates from the current value to the newend.
What changes with explicit animations
With an explicit animation, you take over three jobs that implicit widgets did for you:
- Creating the controller. You construct an
AnimationControllerwith adurationand avsync. - Driving time. You call
forward(),reverse(),repeat(), or setvalue. - Cleaning up. You call
dispose()in the state'sdisposemethod.
In return, you get the controller's value from 0.0 to 1.0, its status, and the ability to share it among many widgets. The next chapters build these up one piece at a time.
Exercise
A product card should shrink slightly while the user's finger is pressed down, and return to full size when released, with the size following how far a drag has moved. Can an implicit animation do this? Name the specific limit involved.
Show answer
Not well. Following the drag distance means the animation's position is set directly by input. Implicit widgets own a private controller and only animate toward a new target, so you can't set their progress. A controller whose value is assigned from the drag is the right tool. A simple press-and-release shrink with fixed timing could still use an implicit widget, because that only changes the target.
Exercise
Why does AnimatedContainer not animate when a screen first appears, and which implicit widget from this tutorial does animate on first build?
Show answer
AnimatedContainer animates only when a property changes between builds, and the first build has no previous value. TweenAnimationBuilder animates from its tween's begin to its end on the first build.