Sub topic 12
Applying curves with CurvedAnimation
This chapter assumes you can write a StatefulWidget with SingleTickerProviderStateMixin. It also assumes you have created and disposed an AnimationController, and that you know a Tween maps the range 0 to 1 onto other values. Here you will learn how a curve reshapes the controller's value, where it sits in the pipeline, and which errors it commonly causes.
What a curve does
An AnimationController produces a value that rises evenly from 0.0 to 1.0 over its duration. Motion with a constant speed looks mechanical. A curve is a function that takes the progress t (0 to 1) and returns a new progress value. The animation still starts at 0 and ends at 1, but the speed in between changes.
CurvedAnimation wraps a parent animation, usually the controller, and applies a Curve to its value:
final curved = CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
);curved is itself an Animation<double>. Anything that accepts an animation accepts it, including Tween.animate, FadeTransition and AnimatedBuilder.
Implicit widgets take a curve: argument directly. With explicit animations you build the CurvedAnimation yourself, which gives you more control, including a different curve for each direction.
Where the curve sits in the pipeline
The order is controller, then curve, then tween, then widget. The curve works on normalized progress. The tween converts the curved progress into a real value such as a size or an offset.
A complete example with a reverse curve
CurvedAnimation accepts a second curve, reverseCurve, which is used while the animation runs backward. This lets a panel bounce in and ease out. The example below runs as shown.
import 'package:flutter/material.dart';
void main() => runApp(
const MaterialApp(
home: Scaffold(body: Center(child: BouncingSlide())),
),
);
class BouncingSlide extends StatefulWidget {
const BouncingSlide({super.key});
@override
State<BouncingSlide> createState() => _BouncingSlideState();
}
class _BouncingSlideState extends State<BouncingSlide>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final CurvedAnimation _curved;
late final Animation<Offset> _offset;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 800),
);
_curved = CurvedAnimation(
parent: _controller,
curve: Curves.bounceOut,
reverseCurve: Curves.easeIn,
);
_offset = Tween<Offset>(
begin: const Offset(-1, 0),
end: Offset.zero,
).animate(_curved);
}
@override
void dispose() {
_curved.dispose();
_controller.dispose();
super.dispose();
}
void _toggle() {
if (_controller.isForwardOrCompleted) {
_controller.reverse();
} else {
_controller.forward();
}
}
@override
Widget build(BuildContext context) {
return Column(
mainAxisSize: MainAxisSize.min,
children: [
SlideTransition(
position: _offset,
child: const FlutterLogo(size: 80),
),
const SizedBox(height: 24),
ElevatedButton(onPressed: _toggle, child: const Text('Toggle')),
],
);
}
}_curved is created once in initState and stored. Creating a new CurvedAnimation inside build makes a new object on every rebuild, which wastes work. In current Flutter versions CurvedAnimation has a dispose() method that removes its listener from the parent. Call it before you dispose the controller.
Common curves
| Curve | Behavior | Typical use |
|---|---|---|
| `Curves.linear` | Constant speed | Progress bars, spinners |
| `Curves.easeIn` | Starts slowly, ends fast | Elements leaving the screen |
| `Curves.easeOut` | Starts fast, ends slowly | Elements entering the screen |
| `Curves.easeInOut` | Slow at both ends | Moving between two on-screen positions |
| `Curves.bounceOut` | Bounces at the end | Playful drops |
| `Curves.elasticOut` | Overshoots, then settles | Emphasis, pop-in |
| `Curves.fastOutSlowIn` | Quick start, long soft landing | Material standard motion |
Every curve also has a mirrored form through .flipped, so Curves.easeIn.flipped behaves like an ease-out.
reverseCurve and status
Flutter chooses between curve and reverseCurve by looking at the parent's status. When the status is reverse, it uses reverseCurve. Two details cause confusion:
- If you leave
reverseCurveasnull, the forwardcurveis used for both directions, and the reverse motion plays the same curve from 1 down to 0. That is not the mirror image of the forward motion. - With
repeat(reverse: true), the status flips betweenforwardandreverseon every cycle, soreverseCurveapplies to every second leg. This is usually what you want for a pulse.
Applying a curve without CurvedAnimation
You can attach a curve to a single tween instead of to the shared controller. CurveTween is an Animatable that applies a curve, and chain combines it with another tween:
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;
late final Animation<double> _scale;
late final Animation<double> _opacity;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 900),
)..forward();
_scale = Tween<double>(begin: 0.5, end: 1.0)
.chain(CurveTween(curve: Curves.elasticOut))
.animate(_controller);
_opacity = CurveTween(curve: Curves.easeIn).animate(_controller);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return FadeTransition(
opacity: _opacity,
child: ScaleTransition(
scale: _scale,
child: const FlutterLogo(size: 100),
),
);
}
}This form has no CurvedAnimation object to dispose. It also lets different properties of one controller use different curves. The cost is that you cannot set a separate reverse curve, because CurveTween has no reverseCurve.
Overshooting curves and the 0 to 1 range
Curves.elasticOut, Curves.bounceOut and the back family return values below 0 or above 1 during the motion. That is fine for scale, offset or rotation. It breaks properties that must stay in a range:
FadeTransitionandOpacityassert that opacity is between 0.0 and 1.0 in debug mode, so an elastic curve on opacity throws an assertion error.Color.lerpandCurves-driven alpha values can produce invalid channels.
In the PopIn example the elastic curve drives the scale and a plain easeIn drives the opacity. If you need an overshooting curve on a bounded value, use .clamp(0.0, 1.0) on the result inside an AnimatedBuilder.
Writing a custom curve
Extend Curve and override transformInternal. The base class's transform already returns t unchanged when it is exactly 0.0 or 1.0, so your function only needs to handle values in between.
import 'dart:math' as math;
import 'package:flutter/animation.dart';
class WobbleCurve extends Curve {
const WobbleCurve({this.wobbles = 3});
final double wobbles;
@override
double transformInternal(double t) {
// Moves steadily from 0 to 1, with a sine wobble that fades out.
return t + math.sin(t * wobbles * 2 * math.pi) * 0.1 * (1 - t);
}
}Use it like any other curve: CurvedAnimation(parent: _controller, curve: const WobbleCurve()). For simple easing shapes, Cubic(x1, y1, x2, y2) gives a cubic Bézier curve and is usually enough.
Common errors
- Passing a
CurvedAnimationwhere aTweenis expected.CurvedAnimationis anAnimation<double>, not anAnimatable. Pass it totween.animate(...), not the other way around. - Applying the curve twice. If you wrap the controller in
CurvedAnimationand also chain aCurveTweenon the same value, the two curves compose and the motion looks distorted. - Expecting the curve to change the duration. A curve changes the speed profile only. The total time stays the controller's
duration. - Creating the curve in
build. This allocates a new object and listener on each rebuild. - Forgetting
dispose. Dispose theCurvedAnimation(if you keep one) and the controller.
Exercise
A FadeTransition driven by CurvedAnimation(parent: _controller, curve: Curves.elasticOut) throws an assertion error in debug mode partway through the animation. Why does this happen, and how would you fix it while keeping the elastic effect on a scale animation?
Show answer
Curves.elasticOut returns values above 1.0 during the overshoot, and opacity must stay between 0.0 and 1.0, so the assertion fails. Keep the elastic curve on the scale, for example with Tween<double>(begin: 0.5, end: 1.0).chain(CurveTween(curve: Curves.elasticOut)).animate(_controller). Drive the opacity with a non-overshooting curve such as CurveTween(curve: Curves.easeIn).animate(_controller).