Animation Controller in Flitter

Animations are a crucial part of creating engaging and dynamic user interfaces. In Flitter, the AnimationController is the core class for managing animations. It allows you to control the duration, speed, and direction of animations, providing a smooth and customizable animation experience.

Understanding AnimationController

AnimationController generates a new value whenever the device’s screen is ready to display a new frame. Typically, this occurs 60 times per second.

Key Concepts

  1. Duration: The length of time the animation should last.
  2. Value: A double that ranges from 0.0 to 1.0, representing the current state of the animation.
  3. Forward: Playing the animation from start to end.
  4. Reverse: Playing the animation from end to start.

Basic Usage

Here’s a simple example of how to create and use an AnimationController:

import {
  StatefulWidget,
  State,
  AnimationController,
  Duration,
} from "@meursyphus/flitter";

class AnimatedWidget extends StatefulWidget {
  createState() {
    return new AnimatedWidgetState();
  }
}

class AnimatedWidgetState extends State<AnimatedWidget> {
  private controller: AnimationController;

  initState() {
    super.initState();
    this.controller = new AnimationController({
      duration: 1000, // milliseconds
      lowerBound: 0,
      upperBound: 1,
    });
    this.controller.forward();
  }

  dispose() {
    this.controller.dispose();
    super.dispose();
  }

  build() {
    return /* Your widget tree here */;
  }
}

In this example, we create an AnimationController with a duration of 1 second and start it immediately in the initState method.

Animating Widgets

To use the AnimationController to animate a widget, you typically use it in combination with a Tween:

import { AnimatedBuilder, Tween, Container } from '@meursyphus/flitter';

// ... inside your build method
build() {
  return AnimatedBuilder({
    animation: this.controller,
    builder: (context, child) => {
      return Container({
        width: Tween({ begin: 0, end: 200 }).evaluate(this.controller),
        height: 100,
        color: 'blue'
      });
    }
  });
}

This will animate the width of the container from 0 to 200 over 1 second.

Advanced Usage

Repeating Animations

You can create repeating animations using the repeat method:

this.controller = new AnimationController({
  duration: 2000, // milliseconds
  lowerBound: 0,
  upperBound: 1,
});
this.controller.repeat(reverse: true);

This creates a ping-pong animation that goes back and forth.

Curved Animations

You can apply non-linear animation curves using the Curve class:

import { CurvedAnimation, Curves } from "@meursyphus/flitter";

const curvedAnimation = new CurvedAnimation({
  parent: this.controller,
  curve: Curves.easeInOut,
});

Staggered Animations

You can create staggered animations by using multiple AnimationControllers with different delays:

this.controller1 = new AnimationController({
  duration: 500, // milliseconds
});
this.controller2 = new AnimationController({
  duration: 500, // milliseconds
});

this.controller1.forward();
this.controller1.addStatusListener((status) => {
  if (status === AnimationStatus.completed) {
    this.controller2.forward();
  }
});

Best Practices

  1. Dispose properly: Always dispose of your AnimationControllers in the dispose method to prevent memory leaks.

  2. Use const constructors: When possible, use const constructors for Tweens and Animations to improve performance.

  3. Keep animations smooth: Aim for 60 fps by keeping your animations simple and optimized.

  4. Test on devices: Always test your animations on real devices to ensure smooth performance.

  5. Use built-in widgets: Flitter provides many pre-built animated widgets like AnimatedContainer, AnimatedOpacity, etc. Use these when possible for simpler code.

Conclusion

The AnimationController is a powerful tool in Flitter for creating smooth, customizable animations. By mastering its use along with concepts like Tweens and Curves, you can create engaging and dynamic user interfaces. Remember to always consider performance and user experience when implementing animations in your Flitter applications.