Sub topic 3
Shaping motion with duration and curves
This chapter assumes you know Dart classes and const constructors, and that you can write a StatefulWidget that calls setState. It also assumes you have seen AnimatedContainer in the previous chapters. Here you will learn how the two settings every implicit animation shares, duration and curve, decide how an animation feels.
Two settings, two questions
Every implicit animation widget answers two questions:
- How long does the change take? This is the
duration, a DartDuration. - How is the time spent? This is the
curve, aCurveobject.
A Duration is the total length of the animation. A curve is a function that reshapes the progress of that time. Two animations with the same duration can feel very different. One moves at constant speed, and the other starts fast and settles gently.
How time becomes a value
On every frame, Flutter computes the raw progress t, which is elapsed time divided by duration. It runs from 0.0 to 1.0. The curve maps t to an eased progress. The widget then blends the old and new property values using that eased progress.
For a width going from 100 to 200, a linear curve gives 150 at the halfway point. Curves.easeIn gives a value below 150 at that moment, because the animation starts slowly. The curve never changes where the animation starts or ends. It changes only how the path between them is covered.
Setting duration
duration is a required argument on implicit widgets such as AnimatedContainer, so you must always choose one.
AnimatedContainer(
duration: const Duration(milliseconds: 300),
width: _big ? 200 : 100,
height: 100,
color: Colors.teal,
)Some practical guidance:
- Small, local changes such as a color change or a toggle usually feel right between 150 and 300 ms.
- Larger movements, such as a panel crossing much of the screen, need more time. Roughly 300 to 500 ms is typical.
- Anything over about a second feels slow unless it is decorative.
Duration.zeroproduces no visible animation, which is useful when you want to disable motion conditionally.
The duration applies to the whole widget. You cannot give the width one duration and the color another inside a single AnimatedContainer. If you need that, use separate widgets or an explicit animation, which a later chapter covers.
Setting a curve
The curve argument takes any Curve. It defaults to Curves.linear, which moves at constant speed and tends to look mechanical. The Curves class provides many ready-made options.
| Curve | Behavior | Typical use |
|---|---|---|
| `Curves.linear` | Constant speed | Progress bars, continuous loops |
| `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 | General-purpose movement |
| `Curves.fastOutSlowIn` | Quick start, long gentle stop | Material-style movement |
| `Curves.bounceOut` | Bounces at the end, stays within 0 to 1 | Playful drops |
| `Curves.elasticOut` | Overshoots the target, then settles | Springy emphasis |
A useful rule is that things entering the screen should decelerate, so use an ease-out curve. Things leaving should accelerate, so use an ease-in curve. Things moving between two visible positions should use ease-in-out.
Comparing curves side by side
Reading about curves helps less than watching them. This app moves one chip per curve from left to right. Run it and press the button.
import 'package:flutter/material.dart';
void main() => runApp(
const MaterialApp(home: Scaffold(body: SafeArea(child: CurveRace()))),
);
class CurveRace extends StatefulWidget {
const CurveRace({super.key});
@override
State<CurveRace> createState() => _CurveRaceState();
}
class _CurveRaceState extends State<CurveRace> {
bool _atEnd = false;
static final Map<String, Curve> _curves = {
'linear': Curves.linear,
'easeIn': Curves.easeIn,
'easeOut': Curves.easeOut,
'easeInOut': Curves.easeInOut,
'bounceOut': Curves.bounceOut,
'elasticOut': Curves.elasticOut,
};
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(16),
child: Column(
children: [
for (final entry in _curves.entries)
SizedBox(
height: 56,
child: AnimatedAlign(
duration: const Duration(milliseconds: 1200),
curve: entry.value,
alignment:
_atEnd ? Alignment.centerRight : Alignment.centerLeft,
child: Chip(label: Text(entry.key)),
),
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () => setState(() => _atEnd = !_atEnd),
child: const Text('Run'),
),
],
),
);
}
}All six chips share one duration and one target. They differ only in curve, and they still arrive at the same moment. The elastic chip passes the right edge before settling back. That overshoot is part of what the curve does.
Curves that leave the 0 to 1 range
bounceOut stays between 0 and 1. elasticOut does not. It briefly produces eased progress above 1.0, and elasticIn goes below 0.0. The widget blends values by extrapolating, so the property can go past its target.
Adjusting and building curves
You rarely need a curve that is not in Curves, but three tools cover most cases.
flippedreverses a curve in time.Curves.easeIn.flippedbehaves like an ease-out. It is not identical toCurves.easeOut, but it mirrors the shape.Cubic(a, b, c, d)defines a cubic Bézier curve through control points. The built-in ease curves areCubicvalues, andCurves.easeInOutisCubic(0.42, 0.0, 0.58, 1.0).Interval(begin, end, curve: ...)holds at 0 untilbegin, applies the curve betweenbeginandend, then holds at 1. A later chapter uses it for staggering.
You can also subclass Curve and override transformInternal. The base class transform method returns exactly 0.0 at t == 0 and 1.0 at t == 1, so your function only needs to handle the values between.
import 'package:flutter/material.dart';
/// A smoothstep curve: gentle at both ends, steeper than easeInOut in the middle.
class SmoothStep extends Curve {
const SmoothStep();
@override
double transformInternal(double t) => t * t * (3 - 2 * t);
}
class SnappyCard extends StatefulWidget {
const SnappyCard({super.key});
@override
State<SnappyCard> createState() => _SnappyCardState();
}
class _SnappyCardState extends State<SnappyCard> {
bool _open = false;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () => setState(() => _open = !_open),
child: AnimatedContainer(
duration: const Duration(milliseconds: 350),
curve: const SmoothStep(),
width: 240,
height: _open ? 160 : 60,
decoration: BoxDecoration(
color: _open ? Colors.indigo : Colors.grey,
borderRadius: BorderRadius.circular(_open ? 24 : 8),
),
),
);
}
}The one curve is applied to height, color, and border radius at the same time.
Common mistakes
- Leaving the curve at linear. Linear motion looks stiff for objects that move across the screen. Choose an ease curve unless the motion is continuous.
- Making durations too long. A 2-second animation on a button feels broken. Test on a real device, and keep interface feedback short.
- Expecting different timing per property. One widget has one duration and one curve. Split the properties across widgets if they must differ.
- Changing the target during an animation. If
setStatechanges the target while an animation runs, the widget restarts from the current value toward the new target. It does not jump. This makes rapid taps look smooth, but the curve restarts, so a strong ease-in can feel sluggish when the user taps repeatedly. - Using out-of-range curves on constrained properties. See the warning above.
Exercise
An AnimatedOpacity widget fades from 0.0 to 1.0 over 500 ms with curve: Curves.elasticOut, and in debug mode it throws an assertion error. Explain why, and name a change that fixes it without changing the duration.
Show answer
elasticOut briefly produces eased progress above 1.0 while it overshoots and settles. The widget blends values with that progress, so the opacity goes above 1.0, and opacity must stay between 0.0 and 1.0. Replace the curve with one that stays within 0 to 1, such as Curves.easeOut or Curves.bounceOut. The duration can stay at 500 ms.