Sub topic 12
Applying curves with CurvedAnimation
This chapter assumes you know Dart classes, mixins, generics, closures, and Flutter’s StatefulWidget, setState, and build method. You should also know that an AnimationController produces a value from 0.0 to 1.0.
Why use CurvedAnimation?
An animation controller usually changes its value at a steady rate. A linear animation moves at the same speed from start to finish. That can feel mechanical.
CurvedAnimation is an animation that changes the controller’s timing. It applies a Curve, such as Curves.easeInOut, to the controller’s progress.
The controller still runs from 0.0 to 1.0, but the curved animation produces values that describe a different pace:
Curves.easeInstarts slowly and speeds up.Curves.easeOutstarts quickly and slows down.Curves.easeInOutstarts and ends slowly.Curves.linearkeeps a constant speed.
The curve changes timing, not the property range. Use a Tween when you need to change the range or type of values.
A curved transition
This example makes a logo grow and shrink. The controller repeats in both directions. CurvedAnimation makes each change ease in and out.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: CurvedLogo()));
}
class CurvedLogo extends StatefulWidget {
const CurvedLogo({super.key});
@override
State<CurvedLogo> createState() => _CurvedLogoState();
}
class _CurvedLogoState extends State<CurvedLogo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _scale;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
)..repeat(reverse: true);
_scale = CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ScaleTransition(
scale: _scale,
child: const FlutterLogo(size: 100),
),
),
);
}
}ScaleTransition expects a value that describes the scale. The animation above ranges from 0.0 to 1.0, so the logo would shrink too far. In practice, combine the curve with a Tween to choose a useful range:
final scale = Tween<double>(begin: 0.8, end: 1.2).animate(
CurvedAnimation(
parent: _controller,
curve: Curves.easeInOut,
),
);Here, the curve controls the pace, while the tween maps progress to scale values between 0.8 and 1.2.
:::callout tip A curve is about when a value changes. A tween is about which values it can have. Use both when an animation needs custom timing and a custom range. :::
Splitting a timeline with Interval
Interval is a curve that uses only part of the controller’s timeline. Its start and end values range from 0.0 to 1.0.
For example, this animation fades during the first half:
final opacity = CurvedAnimation(
parent: _controller,
curve: const Interval(0.0, 0.5, curve: Curves.easeIn),
);The opacity changes while the controller moves from 0.0 to 0.5. It remains at its final value for the rest of the controller’s run. Use different intervals to sequence multiple effects.
Exercise
How would you make a slide animation run during the second half of a controller’s timeline?
Show answer
Wrap the controller in CurvedAnimation with curve: const Interval(0.5, 1.0, curve: Curves.easeOut), then pass that animation to SlideTransition.