Sub topic 14
Ready-made transitions: FadeTransition, ScaleTransition, SlideTransition
This chapter assumes you know Dart classes, Flutter’s build method, StatefulWidget, and basic layout widgets. It also assumes you have seen AnimationController and CurvedAnimation.
A transition widget connects an animation value to one visual property. You provide the animation; Flutter applies the changing value during each frame.
| Widget | Animated value | Typical effect |
|---|---|---|
| `FadeTransition` | `Animation<double>` from 0 to 1 | Fades a child in or out |
| `ScaleTransition` | `Animation<double>` | Grows or shrinks a child |
| `SlideTransition` | `Animation<Offset>` | Moves a child by its own size |
These widgets are useful with explicit animations because an AnimationController supplies values from 0.0 to 1.0, while a CurvedAnimation changes their timing.
A complete example
The following app combines all three transitions. Press the button to play the entrance animation or reverse it.
import 'package:flutter/material.dart';
void main() {
runApp(const TransitionDemo());
}
class TransitionDemo extends StatefulWidget {
const TransitionDemo({super.key});
@override
State<TransitionDemo> createState() => _TransitionDemoState();
}
class _TransitionDemoState extends State<TransitionDemo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _animation;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 700),
);
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
void _toggleAnimation() {
if (_controller.status == AnimationStatus.completed) {
_controller.reverse();
} else {
_controller.forward();
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Transitions')),
body: Center(
child: FadeTransition(
opacity: _animation,
child: ScaleTransition(
scale: Tween<double>(begin: 0.7, end: 1.0).animate(_animation),
child: SlideTransition(
position: Tween<Offset>(
begin: const Offset(0, 0.4),
end: Offset.zero,
).animate(_animation),
child: const FlutterLogo(size: 120),
),
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _toggleAnimation,
child: const Icon(Icons.play_arrow),
),
),
);
}
}The controller still reports values between 0.0 and 1.0. The Tween<double> maps those values to a scale from 0.7 to 1.0. The slide tween maps them from 40% below the logo to its normal position.
SlideTransition uses an Offset measured as a fraction of the child’s size. For example, Offset(1, 0) places the child one child-width to the right. It does not mean one logical pixel.
Common errors
- Passing a plain
doubletoopacityinstead of anAnimation<double>. - Forgetting to dispose the controller.
- Using a scale below zero, which flips the child.
- Expecting
SlideTransitionoffsets to use pixels. - Rebuilding a large static subtree on every animation tick. Pass that subtree as the transition’s
childwhen possible.
Exercise
What does Offset(0, 1) usually mean in a SlideTransition?
Show answer
It places the child one child-height below its normal position.