Sub topic 14
Ready-made transitions: FadeTransition, ScaleTransition, SlideTransition
This chapter assumes you know how to create an AnimationController with SingleTickerProviderStateMixin, how Tween and CurvedAnimation work, and why you dispose a controller. Earlier chapters covered AnimatedBuilder. Here you will see the widgets that wrap the most common AnimatedBuilder patterns for you.
What a transition widget is
A transition widget is a widget that takes an Animation object and applies it to one property of its child. The widget listens to the animation and updates itself on every tick. You do not call setState, and you do not write a builder function.
Most transition widgets extend AnimatedWidget. This base class subscribes to a Listenable (here, an animation) and rebuilds itself when the value changes. Because only the transition widget rebuilds, its child is created once and reused.
The common transition widgets are:
| Widget | Property it drives | Animation type it needs |
|---|---|---|
| `FadeTransition` | `opacity` | `Animation<double>` |
| `ScaleTransition` | `scale` | `Animation<double>` |
| `SlideTransition` | `position` | `Animation<Offset>` |
| `RotationTransition` | `turns` | `Animation<double>` |
| `SizeTransition` | `sizeFactor` | `Animation<double>` |
The argument types matter. SlideTransition does not accept a plain Animation<double>. You must convert it with a Tween<Offset>.
How a transition widget compares with AnimatedBuilder
You can write any of these effects with AnimatedBuilder and Opacity, Transform.scale or FractionalTranslation. The transition widgets give you the same result with less code. They also use render objects that are built for animation. For example, FadeTransition updates opacity at the paint stage and does not rebuild its subtree. Opacity inside an AnimatedBuilder makes Flutter run the builder on every frame.
FadeTransition
FadeTransition takes an opacity animation, whose value runs from 0.0 (invisible) to 1.0 (fully visible). A controller already produces values from 0 to 1, so you can often pass the controller, or a CurvedAnimation built on it, with no Tween.
import 'package:flutter/material.dart';
class FadeIn extends StatefulWidget {
const FadeIn({super.key});
@override
State<FadeIn> createState() => _FadeInState();
}
class _FadeInState extends State<FadeIn> with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 600),
);
late final Animation<double> _opacity = CurvedAnimation(
parent: _controller,
curve: Curves.easeIn,
);
@override
void initState() {
super.initState();
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _opacity,
child: const Text('Welcome back'),
);
}
}Note that CurvedAnimation is an Animation<double>, so it fits the opacity parameter directly.
ScaleTransition
ScaleTransition multiplies the size of the child by the animation value. A value of 1.0 is normal size and 0.0 makes the child vanish. Values above 1.0 enlarge it.
By default the scale is applied around the center of the child. You can change that with the alignment parameter. For example, Alignment.topLeft makes the child grow out of its top-left corner.
Scaling is a paint-time transform. The widget does not change the layout size of the child, so neighbors in a Row or Column do not move. If you want the space to change as well, use SizeTransition.
import 'package:flutter/material.dart';
class PopIn extends StatefulWidget {
const PopIn({super.key});
@override
State<PopIn> createState() => _PopInState();
}
class _PopInState extends State<PopIn> with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 500),
);
late final Animation<double> _scale = Tween<double>(begin: 0.0, end: 1.0)
.animate(CurvedAnimation(parent: _controller, curve: Curves.elasticOut));
@override
void initState() {
super.initState();
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ScaleTransition(
scale: _scale,
alignment: Alignment.center,
child: const Icon(Icons.check_circle, size: 64, color: Colors.green),
);
}
}Curves.elasticOut overshoots 1.0 and settles back, which gives a springy pop. The scale briefly goes above 1.0, which is fine for ScaleTransition. The same curve on a FadeTransition would be a bug, because opacity outside 0.0 to 1.0 triggers an assertion.
SlideTransition
SlideTransition moves the child by an Offset. The unit of that offset is the child's own size, not logical pixels.
Offset(1, 0)moves the child right by one full child width.Offset(0, -0.5)moves it up by half its height.Offset.zeroleaves it where it is.
This makes slide animations independent of screen size. A panel that starts at Offset(0, 1) always starts just below its final position, however tall it is.
You need a Tween<Offset> to produce the Animation<Offset>.
import 'package:flutter/material.dart';
class SlideUpBanner extends StatefulWidget {
const SlideUpBanner({super.key});
@override
State<SlideUpBanner> createState() => _SlideUpBannerState();
}
class _SlideUpBannerState extends State<SlideUpBanner>
with SingleTickerProviderStateMixin {
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 350),
);
late final Animation<Offset> _position = Tween<Offset>(
begin: const Offset(0, 1),
end: Offset.zero,
).animate(CurvedAnimation(parent: _controller, curve: Curves.easeOutCubic));
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
ElevatedButton(
onPressed: () {
if (_controller.isCompleted) {
_controller.reverse();
} else {
_controller.forward();
}
},
child: const Text('Toggle banner'),
),
ClipRect(
child: SlideTransition(
position: _position,
child: Container(
padding: const EdgeInsets.all(16),
color: Colors.amber,
child: const Text('Saved'),
),
),
),
],
);
}
}SlideTransition does not clip. At the start of the animation the banner sits one full height below its slot and would draw over whatever is underneath. ClipRect hides the part that sits outside the slot.
The widget also accepts textDirection and transformHitTests. When textDirection is TextDirection.rtl, the x component flips, so Offset(1, 0) moves the child to the left. transformHitTests is true by default, which means taps follow the child's visual position.
Combining transitions
Transition widgets nest. Each one drives one property, so you combine effects by wrapping one in another and sharing a single controller.
import 'package:flutter/material.dart';
Widget buildCard(Animation<double> animation, Widget child) {
final slide = Tween<Offset>(
begin: const Offset(0, 0.2),
end: Offset.zero,
).animate(CurvedAnimation(parent: animation, curve: Curves.easeOut));
return FadeTransition(
opacity: animation,
child: SlideTransition(position: slide, child: child),
);
}Here animation is passed as the opacity directly and also as the parent of a second CurvedAnimation. Both widgets read the same controller value, so they stay in sync.
Using Animatable.animate and chain
A Tween is an Animatable. Calling tween.animate(parent) returns an Animation. You can also chain animatables with chain, which applies a curve to a tween without creating a CurvedAnimation object.
import 'package:flutter/material.dart';
final Animatable<Offset> slideTween = Tween<Offset>(
begin: const Offset(-1, 0),
end: Offset.zero,
).chain(CurveTween(curve: Curves.easeOut));You then call slideTween.animate(_controller). A CurveTween is useful when the same tween is reused with several controllers.
Common errors
- Passing an
Animation<double>toSlideTransition.position. The compiler reports a type mismatch. Wrap the value in aTween<Offset>. - Using a bouncing or elastic curve with
FadeTransition. Opacity outside 0.0 to 1.0 fails an assertion. UseCurves.easeIn, or apply.clamp(0.0, 1.0)through aTween. - Forgetting
forward(). A controller starts at0.0and does nothing until you start it. The child then stays invisible, or off-screen, which looks like a layout bug. - Creating the animation in
build. A newCurvedAnimationorTweenon every build wastes work and can leak listeners. Create it once ininitStateor in alate finalfield. - Missing
ClipRecton slides. The child draws outside its slot during the animation and overlaps neighbors. - Expecting layout to change.
ScaleTransitionandSlideTransitiondo not affect layout. Neighbors keep their positions.
When to prefer AnimatedBuilder
Use a transition widget when one property of one child needs animating and a built-in widget exists for it. Use AnimatedBuilder when the effect has no built-in widget, such as animating a Color or a custom painter value, or when one animation drives several properties of the same widget.
Exercise
A banner should slide in from the left edge of its own slot, starting fully outside it, and end at its normal position. Which Tween<Offset> begin and end values do you use, and why do you wrap the transition in ClipRect?
Show answer
Use Tween<Offset>(begin: const Offset(-1, 0), end: Offset.zero). The offset unit is the child's own width, so -1 places the child exactly one width to the left. SlideTransition does not clip its child, so without ClipRect the banner would draw over neighboring widgets while it is outside its slot.
Exercise
You apply Curves.elasticOut to the opacity animation of a FadeTransition. What happens, and how do you get a springy effect on the same widget safely?
Show answer
The curve overshoots above 1.0, so the opacity value leaves the allowed range of 0.0 to 1.0 and an assertion fails in debug mode. Apply the elastic curve to a ScaleTransition instead, and keep a curve such as Curves.easeIn on the FadeTransition.