What is the ColorTween animation in Flutter?
Tween animation in Flutter includes creating an intermediate state between the two endpoints, a starting state and an ending state. If you are moving a Tween animation for a smoother transition between two states.
How to define the ColorTween
Tween is a stateless object which generates a set of new values based on the specified start point, end point, and a cycle duration.
Given below is the syntax of defining a Tween.
// Declaration of TweencolorAnimation = ColorTween(begin: Colors.x, end: Colors.y);// Use of Tweendouble value = colorAnimation.transform(0.5);
Here,
Line 2: The
colorAnimationis aTweenobject which represents the start and end color of the object.Line 5: The
transform()method is called with the value set to0.5. It determines the time period of one animation cycle.
Note:
The greater the duration value, the longer the animation will take to complete one cycle. The smaller the value, the faster the animation will complete one cycle.
Moreover,
0.5is not the value to which the points are incremented. It is the time period for one cycle completion.
Code
Details on how to use the ColorTween animation along with the code is given below.
import 'package:flutter/material.dart';
void main()
{
runApp(MyApp());
}
class MyApp extends StatelessWidget
{
@override
Widget build(BuildContext context)
{
return MaterialApp(home: Scaffold(body: Center(child: TweenAnimation(),),),);
}
}
class TweenAnimation extends StatefulWidget
{
@override
TweenAnimationState createState() => TweenAnimationState();
}
class TweenAnimationState extends State<TweenAnimation> with SingleTickerProviderStateMixin
{
AnimationController controller;
Animation<Color> colorAnimation;
@override
void initState()
{
super.initState();
controller = AnimationController(duration: const Duration(seconds: 2), vsync: this,)..repeat(reverse: true);
colorAnimation = ColorTween(begin: Colors.red, end: Colors.purple,).animate(controller);
}
@override
Widget build(BuildContext context)
{
return AnimatedBuilder(animation: controller, builder: (context, child)
{
return Container(width: 200, height: 200, color: colorAnimation.value,);
},);
}
@override
void dispose()
{
controller.dispose();
super.dispose();
}
}Explanation
Line 3–6: The
runApp()makes theWidgetthe root of the .widget tree A tree that holds all your widgets within each other. It is how you structure your User Interface (UI). Lines 8–15: A new class named
MyAppis defined which extends thestatelessWidgetclass.MaterialAppprovides features such asScaffold(A basic layout to design applications), aCenterwidget (to center the child widgets), and aTweenAnimation().Lines 17–21: Here, we define the class named
TweenAnimationwhich extends theStatefulWidgetclass (A widget that changes state over time).Line 20:
createState()method creates an instance of the class namedTweenAnimationState.Lines 23: The state class
TweenAnimationStateextends theTweenAnimationclass which is created bySingleTickerProviderStateMixin. It is a mixin which is used to createTickerto control animations.Line 25:
controlleris being declared and initialized to to thedurationof2seconds. So, the total time period for the animation to complete is2seconds.vsync.thismakes theanimationControllersynchronized.It is used to inform
controllerabout the screen updates to play animations accordingly...repeat(reverse: true)reverses the direction after the completion of each animation cycle. It causes the animation to repeat infinitely.
Line 26 & 33:
colorAnimationis being declared and initialized with the start state ofredcolor and an end state ofpurplecolor. The square changes its color fromredtopurplewithin the completion of eachdurationcycle.animate()is called to play thecolorTweenanimation.Line 31:
initStateis called only once to initialize the state.Line 37:
buildis a method in thestateclass. It takes the objectBuildContextand returns a widget.Line 39–41:
AnimatedBuilderwidget listens to thecontrolleranimation and calls thebuilder. It eventually returns aContainerwidget with the updatedLine 41: The
widthandheightof the square is set to200which remains constant over theduration. Thecoloris set tocolorAnimation.valuewhich is initialized in line 33.Lines 46–49:
dispose()method is used to prevent any memory leaks. It cleans up theControllerwhenever thestateobject is removed.
Learn more about Flutter
Free Resources