Flutter / Flutter Animation

How to Use Animation Controller in Flutter

In this tutorial, we'll dive into the Animation Controller in Flutter. We'll learn how to use this powerful tool to create and manage animations in Flutter.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explore how to add animations to enhance the user experience.

1. Introduction

1.1 Goal of the tutorial

This tutorial aims to introduce the Animation Controller in Flutter. We will learn how to use this powerful tool to create and manage animations in our Flutter applications.

1.2 Learning outcomes

By the end of this tutorial, you will be able to:

  • Understand the role of the Animation Controller in Flutter
  • Create and manage animations using the Animation Controller
  • Implement custom animations in your Flutter applications

1.3 Prerequisites

Before starting this tutorial, you should have:

  • A basic understanding of Flutter
  • Flutter SDK installed on your system
  • An IDE such as Visual Studio Code or Android Studio

2. Step-by-Step Guide

2.1 Understanding the Animation Controller

In Flutter, the Animation Controller is a type of Animation that can stop, reverse, and restart animations. It requires a Duration to determine how long the animation should last.

2.2 Creating an Animation Controller

First, you need to create an instance of the AnimationController. This is usually done in the initState() method of your StatefulWidget.

AnimationController controller;

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

In the above code, vsync stands for "visual sync". It is an optional property that prevents off-screen animations from consuming unnecessary resources.

3. Code Examples

3.1 Basic Animation

AnimationController controller;
Animation<double> animation;

@override
void initState() {
  super.initState();
  controller = AnimationController(
    duration: const Duration(seconds: 2),
    vsync: this,
  );

  animation = Tween<double>(begin: 0, end: 300).animate(controller)
    ..addListener(() {
      setState(() {
        // This causes the widget to rebuild every time the animation changes value.
      });
    });

  controller.forward();
}

In this example, we use the Tween class to define a range for the animation. The animate() method returns an Animation object, which we then add a listener to. This listener calls setState() every time the animation value changes, causing the widget to rebuild.

3.2 Reverse Animation

controller.reverse();

The reverse() method will play the animation in reverse from its current position.

4. Summary

In this tutorial, we've learned about the Animation Controller in Flutter and how to use it to create custom animations. We've explored how to initiate, reverse, and manage animations, and we've seen how changes in the animation trigger widget rebuilding.

5. Practice Exercises

5.1 Exercise 1

Create a simple animation where an object moves from the left side of the screen to the right.

5.2 Exercise 2

Enhance the previous exercise by making the object move back to the left side of the screen once it reaches the right.

Solutions

5.1 Solution to Exercise 1

AnimationController controller;
Animation<double> animation;

@override
void initState() {
  super.initState();
  controller = AnimationController(
    duration: const Duration(seconds: 2),
    vsync: this,
  );

  animation = Tween<double>(begin: 0, end: 300).animate(controller)
    ..addListener(() {
      setState(() {
      });
    });

  controller.forward();
}

5.2 Solution to Exercise 2

AnimationController controller;
Animation<double> animation;

@override
void initState() {
  super.initState();
  controller = AnimationController(
    duration: const Duration(seconds: 2),
    vsync: this,
  );

  animation = Tween<double>(begin: 0, end: 300).animate(controller)
    ..addListener(() {
      setState(() {
      });
    });

  controller.forward().then((_) {
    controller.reverse();
  });
}

In the second exercise, we use the then() method to wait for the forward animation to finish before starting the reverse animation.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

Image Converter

Convert between different image formats.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help