Tween animations help you smoothly change a value over time. This makes your app feel alive and fun.
Tween animations in Flutter
Introduction
Syntax
Flutter
Tween<Type>(begin: startValue, end: endValue)
Tween means 'in-between'. It creates values between start and end.
You use it with an AnimationController to control the timing.
Examples
Flutter
Tween<double>(begin: 0.0, end: 1.0)
Flutter
Tween<Color?>(begin: Colors.red, end: Colors.blue)
Flutter
Tween<Offset>(begin: Offset(0, 0), end: Offset(100, 0))
Sample App
This app shows a square that smoothly changes color from red to blue and back. It uses a ColorTween with an AnimationController that repeats forever.
Flutter
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: TweenAnimationExample(), ); } } class TweenAnimationExample extends StatefulWidget { const TweenAnimationExample({super.key}); @override State<TweenAnimationExample> createState() => _TweenAnimationExampleState(); } class _TweenAnimationExampleState extends State<TweenAnimationExample> with SingleTickerProviderStateMixin { late final AnimationController _controller; late final Animation<Color?> _colorAnimation; @override void initState() { super.initState(); _controller = AnimationController( duration: const Duration(seconds: 2), vsync: this, ); _colorAnimation = ColorTween(begin: Colors.red, end: Colors.blue).animate(_controller); _controller.repeat(reverse: true); } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Tween Animation Example')), body: Center( child: AnimatedBuilder( animation: _controller, builder: (context, child) { return Container( width: 100, height: 100, color: _colorAnimation.value, ); }, ), ), ); } }
Important Notes
Always dispose your AnimationController to free resources.
You can tween many types: numbers, colors, positions, and more.
Use AnimatedBuilder to rebuild widgets when the animation changes.
Summary
Tweens create smooth changes between two values.
Use them with an AnimationController to control timing.
They make your app feel smooth and alive.
Practice
1. What is the main purpose of a
Tween in Flutter animations?easy
Solution
Step 1: Understand Tween's role
A Tween defines the start and end values for an animation, like from 0 to 100.Step 2: Differentiate from controller
The AnimationController controls timing, not the value range.Final Answer:
To define the range of values an animation will interpolate between -> Option CQuick Check:
Tween = value range [OK]
Hint: Tween sets value range, controller sets timing [OK]
Common Mistakes:
- Confusing Tween with AnimationController
- Thinking Tween controls animation speed
- Assuming Tween displays animation
2. Which of the following is the correct way to create a Tween from 0.0 to 1.0 in Flutter?
easy
Solution
Step 1: Recall Tween constructor parameters
The Tween constructor uses named parametersbeginandendto set start and end values.Step 2: Check options for correct syntax
Only Tween(begin: 0.0, end: 1.0) usesbeginandendcorrectly with valid values.Final Answer:
Tween(begin: 0.0, end: 1.0) -> Option AQuick Check:
Correct parameters = begin, end [OK]
Hint: Use begin and end for Tween values [OK]
Common Mistakes:
- Using wrong parameter names like start or from
- Using invalid syntax like 'range: 0 to 1'
- Mixing integer and double types incorrectly
3. Given this code snippet, what will be the value of
animation.value when the controller is halfway through its duration?
final controller = AnimationController(vsync: this, duration: Duration(seconds: 2)); final animation = Tween(begin: 10.0, end: 20.0).animate(controller); controller.value = 0.5;
medium
Solution
Step 1: Understand Tween interpolation
The Tween goes from 10.0 to 20.0, so halfway means halfway between these values.Step 2: Calculate halfway value
Halfway is (10.0 + 20.0) / 2 = 15.0.Final Answer:
15.0 -> Option BQuick Check:
Halfway value = 15.0 [OK]
Hint: Halfway means average of begin and end [OK]
Common Mistakes:
- Using controller.value directly instead of interpolated value
- Choosing begin or end value instead of midpoint
- Adding values incorrectly
4. What is wrong with this Flutter Tween animation code?
final controller = AnimationController(vsync: this, duration: Duration(seconds: 1)); final animation = Tween(begin: 0.0, end: 100.0).animate(controller); controller.forward(); print(animation.value);
medium
Solution
Step 1: Check animation timing
Calling controller.forward() starts animation asynchronously; animation.value won't update instantly.Step 2: Understand print timing
Printing animation.value immediately after forward() shows the initial value, not the animated value.Final Answer:
Animation value is not updated immediately after forward() -> Option AQuick Check:
Animation updates over time, not instantly [OK]
Hint: Animation updates asynchronously after forward() [OK]
Common Mistakes:
- Expecting animation.value to change immediately
- Confusing vsync errors with animation timing
- Ignoring controller lifecycle management
5. You want to animate a color change smoothly from red to blue over 3 seconds using a Tween. Which of the following code snippets correctly sets up the Tween animation?
hard
Solution
Step 1: Identify correct Tween type for colors
Flutter providesColorTweenspecifically for color interpolation.Step 2: Check constructor parameters
ColorTweenusesbeginandend, notstartorfinish.Final Answer:
final animation = ColorTween(begin: Colors.red, end: Colors.blue).animate(controller); -> Option DQuick Check:
Use ColorTween with begin and end [OK]
Hint: Use ColorTween with begin/end for colors [OK]
Common Mistakes:
- Using generic Tween for colors without specifying type
- Using wrong parameter names like start or finish
- Not using ColorTween for color animations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
