Creating Tween Animation in Flutter

Tutorial 1 of 5

Creating Tween Animation in Flutter

1. Introduction

In this tutorial, we will learn how to create Tween Animations in Flutter. Tween stands for in-betweening and is a technique in which intermediate frames are created between two images to give the appearance of the first image evolving smoothly into the second image. We will be using one type of Tween animation, which is the ColorTween animation.

After completing this tutorial, you will be able to create a Flutter app with Tween animations, understand how to transition smoothly between two values over a set period, and have a deeper understanding of Flutter animations.

Prerequisites:
- Basic knowledge of Dart and Flutter.
- Flutter SDK installed on your machine.
- A code editor like VS Code or Android Studio installed.

2. Step-by-Step Guide

Flutter provides us with the Tween class which we can use to specify the start and end points of our animations. We can then animate these properties over time using an AnimationController.

The animation system in Flutter is based on typed Animation objects. Widgets can either incorporate these animations in their build functions directly by reading their current value and listening to their state changes or they can use the animations as the basis of more elaborate animations that they pass along to other widgets.

2.1 Defining a Tween

A Tween is defined with beginning and end values (start and end points). This could be a double, a Color, an Offset, or any other type of object that your animation requires.

Tween<double>(begin: 0, end: 1)

2.2 Animation Controller

The AnimationController is a special Animation object that generates a new value whenever the hardware is ready for a new frame. It requires a vsync which makes sure you don’t use unnecessary resources if the app is not currently visible.

final AnimationController controller = AnimationController(
    duration: const Duration(seconds: 2), 
    vsync: this,
);

2.3 Animate the Tween

To create the animation, call the Tween object's animate method, which requires the parent AnimationController object.

final Animation<double> animation = Tween<double>(begin: 0, end: 1).animate(controller);

3. Code Examples

Let's look at a basic example of a Tween animation that makes a Flutter logo spin and change color.

import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: Scaffold(
        body: Center(
          child: TweenAnimation(),
        ),
      ),
    ),
  );
}

class TweenAnimation extends StatefulWidget {
  @override
  _TweenAnimationState createState() => _TweenAnimationState();
}

class _TweenAnimationState extends State<TweenAnimation> with SingleTickerProviderStateMixin {
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      duration: const Duration(seconds: 5),
      vsync: this,
    )..repeat();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (BuildContext context, Widget child) {
        return Transform.rotate(
          angle: _controller.value * 2.0 * 3.1416,
          child: Opacity(
            opacity: _controller.value,
            child: FlutterLogo(
              size: 200.0,
            ),
          ),
        );
      },
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}

In this example, we first create our AnimationController in the initState method, and start it animating with the repeat() method. In our build method, we use the AnimatedBuilder widget, which rebuilds each time the _controller's value changes.

4. Summary

In this tutorial, we learned how to create Tween animations in Flutter. We went over the Tween class, AnimationController, and how to animate the Tween using the animate method. After completing this tutorial, you should be able to create and animate your own Tween animations.

Next, I recommend you try creating different types of animations using Tween. The Flutter documentation has great resources on this topic.

5. Practice Exercises

  1. Create an animation where a square expands and contracts continuously.
  2. Create an animation where a color of a square changes from red to green over 5 seconds.
  3. Create an animation where a square moves from the top left of the screen to the bottom right.

Solutions:

  1. For the first exercise, you could use a Tween<double> with a begin value of 50.0 and an end value of 200.0 to animate the height and width of a Container.
  2. For the second exercise, you could use a ColorTween to animate the color of a Container from red to green.
  3. For the third exercise, you could use a Tween<Offset> to animate the Transform.translate of a Container.

Remember to dispose your AnimationController in the dispose method to free up resources when they are no longer needed.