Sub topic 3
Shaping Motion with Duration and Curves
This chapter assumes you know Dart classes, Flutter’s build method, StatefulWidget, setState, and basic layout widgets. You will use those ideas to control how an implicit animation feels.
Duration controls time
An animation changes a property from its old value to its new value over a period. Duration specifies that period.
For example, a 400-millisecond animation takes about 0.4 seconds to complete. Flutter calculates intermediate values for each rendered frame and rebuilds the animated widget with those values.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: MotionExample()));
}
class MotionExample extends StatefulWidget {
const MotionExample({super.key});
@override
State<MotionExample> createState() => _MotionExampleState();
}
class _MotionExampleState extends State<MotionExample> {
bool _expanded = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Motion')),
body: Center(
child: GestureDetector(
onTap: () => setState(() => _expanded = !_expanded),
child: AnimatedContainer(
duration: const Duration(milliseconds: 400),
curve: Curves.easeInOut,
width: _expanded ? 220 : 120,
height: _expanded ? 160 : 80,
alignment: Alignment.center,
color: _expanded ? Colors.teal : Colors.orange,
child: const Text('Tap me'),
),
),
),
);
}
}When _expanded changes, AnimatedContainer compares its old and new properties. It animates the differences instead of changing them immediately.
| Duration | Typical use | Risk |
|---|---|---|
| 100–200 ms | Small state changes, such as button feedback | May feel abrupt |
| 250–500 ms | Panels, cards, and size changes | Usually a useful default |
| 600 ms or more | Large entrances or dramatic movement | May feel slow |
Duration is not a performance setting. A longer duration does not make an expensive build cheaper. It only spreads the visual change over more time. If frames are missed, the animation can still appear uneven.
Curves shape progress
A curve maps the animation’s linear progress to a new progress value. At 50 percent of the duration, a linear animation is 50 percent complete. An easing curve may be ahead of or behind that value.
Curves.linearmoves at a constant rate.Curves.easeInstarts slowly and accelerates.Curves.easeOutstarts quickly and decelerates.Curves.easeInOutstarts and ends slowly.Curves.bounceOutovershoots with a bounce and is best for playful interfaces.
Use curves to communicate the type of change. An easeOut curve often works well when an element enters the screen because it settles into place. Avoid bounce or elastic curves for actions that need to feel precise.
Choosing values
Start with a moderate duration and a standard curve. Test the animation on a physical device or emulator. Reduce the duration if the interface feels sluggish. Choose a different curve if the movement has the wrong character, even when its speed is appropriate.
Exercise
An AnimatedContainer feels too slow and stops abruptly. Which settings would you try first?
Show answer
Try a shorter duration, such as 250–350 milliseconds, and replace Curves.linear with Curves.easeOut or Curves.easeInOut.