Sub topic 11
Mapping Values with Tween
Prerequisites
This chapter assumes you understand Dart classes, generics, and closures. You should also know how StatefulWidget, setState, and AnimationController work. An animation controller supplies a changing value between 0.0 and 1.0. A Tween maps that value to a useful range or type.
What a Tween does
A Tween<T> describes a value's starting point and ending point:
Tween<double>(begin: 60, end: 180)When the controller is at 0.0, the tween produces 60. When the controller is at 1.0, it produces 180. Values between those points are interpolated for each animation frame.
The result is an Animation<T>. The animation has a changing value that you can give to a widget property.
A tween does not start, stop, or schedule frames. The controller does that work. The tween only answers this question: “Given the current progress, what value should I use?”
| Tween type | Example | Common use |
|---|---|---|
| `Tween<double>` | `0.0` to `1.0` | Opacity, scale, position |
| `ColorTween` | Orange to teal | Color changes |
| `IntTween` | `0` to `10` | Whole-number values |
| `SizeTween` | Small size to large size | `Size` properties |
A complete example
This example maps the controller's progress to a box size:
import 'package:flutter/material.dart';
void main() => runApp(const TweenDemoApp());
class TweenDemoApp extends StatelessWidget {
const TweenDemoApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: TweenDemo());
}
}
class TweenDemo extends StatefulWidget {
const TweenDemo({super.key});
@override
State<TweenDemo> createState() => _TweenDemoState();
}
class _TweenDemoState extends State<TweenDemo>
with SingleTickerProviderStateMixin {
late final AnimationController _controller;
late final Animation<double> _size;
@override
void initState() {
super.initState();
_controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
);
_size = Tween<double>(begin: 60, end: 180).animate(_controller);
_controller.forward();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: AnimatedBuilder(
animation: _size,
builder: (context, child) {
return Container(
width: _size.value,
height: _size.value,
color: Colors.teal,
);
},
),
),
);
}
}_size.value is the current interpolated size. At the beginning it is 60, and at the end it is 180.
A tween can also map non-numeric values. For example:
final color = ColorTween(
begin: Colors.orange,
end: Colors.teal,
).animate(_controller);Use the tween whose type matches the property you want to animate. For custom types, interpolation is only possible when the type provides a meaningful way to calculate values between its beginning and ending states.
Exercise
If a controller's value is 0.5 and the tween is Tween<double>(begin: 20, end: 100), what value does the tween produce?
Show answer
It produces 60, halfway between 20 and 100.