Animations in Flutter | flutter_animate package

Animations in Flutter | flutter_animate package

 If one of the factors that makes a mobile application attractive is its design, then the second such factor is animations. Everyone will agree that if all elements in the application remain "dry", it looks better with some animation.

With Flutter, you can easily add animations without installing multiple packages, for example, using ready-made widgets (AnimatedContainer, AnimatedCrossFade, etc.). For the convenience of adding animations, we will discuss the flutter_animate package in this article.

First, let's look at a page that simply shows a container.

class MyPage extends StatelessWidget {
  const MyPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('flutter_animate | Package blog'),
        centerTitle: true,
      ),
      body: Center(
        child: Container(
          width: 150,
          height: 150,
          decoration: BoxDecoration(
            color: Colors.red,
            borderRadius: BorderRadius.circular(14)
          ),
        ),
      ),
    );
  }
}

 

As seen in the image, there is a red container in the middle of the page, and there is no animation. Now let's use the flutter_animate package to make the red container appear with animation when the page opens. To do this, first, let's add the flutter_animate package.


flutter pub add flutter_animate

After adding the package, let's make changes to the code.

As a result, the code will look like this.

 


class MyPage extends StatelessWidget {
	const MyPage({super.key});

	@override
	Widget build(BuildContext context) {
		return Scaffold(
		  appBar: AppBar(
		    title: const Text('flutter_animate | Package blog'),
		    centerTitle: true,
		  ),
		  body: Center(
		    child: Container(
		      width: 150,
		      height: 150,
		      decoration: BoxDecoration(
		        color: Colors.red,
		        borderRadius: BorderRadius.circular(14)
		      ),
		    ).animate().fadeIn(duration: 500.ms).slideX(duration: 700.ms, curve: Curves.easeInCubic),
		  ),
		);
	}
}

 

video_2024-02-06_11-40-39.mp4 [video-to-gif output image]

Here we added .animate().fadeIn(duration: 500.ms).slideX(duration: 700.ms, curve: Curves.easeInCubic) to the Container widget. .animate() sets the animation parameters, fadeIn() makes the widget slowly appear (changing opacity from 0 to 1), and slideX() shows its movement along the X-axis from left to right. We also specified the duration to comfortably observe the animation. When you deploy the mobile application on a real device, you can adjust these numbers accordingly.

And here's the result.

For more detailed information, you can watch the video below.

 

Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY Scorpion IT Company SCORPION IT COMPANY