Learn Flutter Animation using Hooks (useEffect and useAnimationController)

24 Jun 2023 Balmiki Mandal 0 Andriod

Flutter Hooks Tutorial: Flutter Animation using Hooks (useEffect and useAnimationController)

Flutter is a great development platform for creating interactive and intuitive mobile applications. With the ability to create sophisticated user interfaces with its powerful widget system, developers can create stunning apps in no time. One of the most exciting features of Flutter is its support for animation. Animations add life and energy to your app, making it more interactive and engaging.

In this tutorial, we’ll learn how to use Flutter Hooks to create animations in our Flutter apps. Specifically, we’ll be looking at the useEffect and useAnimationController hooks to create an animated logo that fades in on screen.

Prerequisites

This tutorial assumes you have some knowledge of working with Flutter and understand the basics of Hooks. If you are unfamiliar with Hooks, please see the Flutter Cookbook for more information.

Getting Started

First, let’s create a new Flutter project and name it “animated_logo”. This will be the project we’ll use as our starting point. Open up the project and navigate to the main.dart file. This is the starting point for all Flutter apps and is where we’ll put our code.

We’ll first need to import the necessary packages. We’ll need the useEffect and useAnimationController Hooks from the flutter_hooks package, which can be downloaded through pub. Add the following code to main.dart:

import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';

Now, we’re ready to begin working with our Hooks. Let’s create a simple widget with the useEffect Hook that will display an animated logo.

Creating the UseEffect Hook

The useEffect Hook allows us to execute code when a certain condition is met. In this case, we want to display our logo when the app is ready. To do this, add the following code to main.dart:

Widget build(BuildContext context) {
  useEffect(() {
    // Code to execute when our App is ready
  });

  return Container();
 }

Inside the useEffect Hook, we’ll add our code for displaying the logo. To do this, we’ll need an Image widget and an AnimatedOpacity widget. The Image widget will display our logo and the AnimatedOpacity widget will allow us to animate our logo.

First, let’s create an Image widget and set its source to our logo. For this example, we’ll use the Flutter logo, but feel free to use your own logo if desired.

Image(
  image: AssetImage('assets/logo.png'),
);

Next, we’ll wrap the Image widget in an AnimatedOpacity widget. The AnimatedOpacity widget takes two parameters: the opacity of the widget and the duration of the animation.

AnimatedOpacity(
  opacity: 0.5,
  duration: Duration(seconds: 1),
  child: Image(
    image: AssetImage('assets/logo.png'),
  ),
);

Finally, we’ll use the useAnimationController Hook to control the animation. This hook takes two parameters: the duration of the animation and the animation curve. For this example, we’ll set the duration to one second and use the Curves.easeInCurve for our animation curve.

useAnimationController(
  duration: Duration(seconds: 1),
  curve: Curves.easeIn,
);

Combining these components together, we get the following code for our useEffect hook:

useEffect(() {
  useAnimationController(
    duration: Duration(seconds: 1),
    curve: Curves.easeIn,
  );
  
  return AnimatedOpacity(
    opacity: 0.5,
    duration: Duration(seconds: 1),
    child: Image(
      image: AssetImage('assets/logo.png'),
    ),
  );
});

If everything has been coded correctly, our animated logo should be displayed on screen when the app launches.

Conclusion

Congratulations! You’ve successfully created an animated logo using Flutter Hooks. This tutorial only scratched the surface of what’s possible with Flutter Hooks. With the power of the useEffect and useAnimationController Hooks, you can create complex and beautiful animations for your Flutter apps.

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.