What is the SizeTween 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 the two states.
How to define the SizeTween
Tween is a duration.
Given below is the syntax of defining a Tween.
// Declaration of TweensizeAnimation = Tween<double>(begin: x, end: y);// Use of Tweendouble value = sizeAnimation.transform(0.5);
Here,
Line 2: The
sizeAnimationis aTweenobject which represents the start and end size 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 SizeTween 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<double> sizeAnimation;
@override
void initState()
{
super.initState();
controller = AnimationController(duration: const Duration(seconds: 2), vsync: this,)..repeat(reverse: true);
sizeAnimation = Tween<double>(begin: 200, end: 50,).animate(controller);
}
@override
Widget build(BuildContext context)
{
return AnimatedBuilder(animation: controller, builder: (context, child)
{
return Container(width: sizeAnimation.value, height: sizeAnimation.value, color: Colors.red,);
},);
}
@override
void dispose()
{
controller.dispose();
super.dispose();
}
}Explanation
Lines 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–51: The state class
TweenAnimationStateextends theTweenAnimationclass which is created bySingleTickerProviderStateMixin. It is a mixin which is used to create theTickerto control animations.Lines 25–26: An instance variable
AnimationControlleris declared to control the animationduration.sizeAnimationis declared to control the size of the object and it has a value ofdouble. The square will change its size from200to50during each animation cycle as initialized in line 33.Line 31:
initStateis called only once to initialize the state.Line 32: The
controlleranimation is initialized to to thedurationof 2 seconds. The total time period for the animation to complete is 2 seconds.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 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 updated values ofsizeAnimationused to set thewidthandheightof the container. Thecoloris set tored.Line 46–49:
dispose()method is used to prevent any memory leaks. It cleans up theControllerwhenever thestateobject is removed.
Learn more about Flutter
Free Resources