Flutter - Animate Image Rotation
Last Updated :
14 Dec, 2023
In Flutter, Image.assets() or Image.network() Widget is used to display images from locally or from the URL. Images can be locally stored in the program or fetched from a network and can be displayed using the Image Widget. Animations can be applied to Images via many techniques. Hence In this article, we are going to apply animation to the Image Rotation which will give a smooth rotation of the image. A sample video is given below to get an idea about what we are going to do in this article.
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Import the Package
First of all import material.dart file.
import 'package:flutter/material.dart';
Step 3: Execute the main Method
Here the execution of our app starts.
Dart
void main() {
runApp(MyApp());
}
Step 4: Create MyApp Class
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
Dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
Step 5: Create MyHomePage Class
In this class, the initState method initializes the animation controller and defines an animation using a Tween. The Tween specifies the starting and ending values for the rotation angle. The Transform.rotate widget is used to rotate the image based on the current value of the animation.Basically in this class we are going to implement the main logic for apply animation to the rotation of the image. Comments are added for better understainding.
AnimatedBuilder(
animation: _animation,
builder: (context, child) {
// Use Transform.rotate to rotate the
// Image based on the animation value
return Transform.rotate(
angle: _animation.value,
child: Image.asset(
'assets/1.png', // Replace with your image asset
width: 200,
height: 200,
),
);
},
),
Dart
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
// Create an animation controller
_controller = AnimationController(
vsync: this, // vsync is set to this for performance reasons
duration: Duration(seconds: 2), // Set the duration of the animation
);
// Create a Tween for the rotation angle
_animation = Tween<double>(
begin: 0, // Start rotation angle
end: 2 * 3.141, // End rotation angle (2 * pi for a full circle)
).animate(_controller);
// Repeat the animation indefinitely
_controller.repeat();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Rotation Animation'),
),
body: Center(
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
// Use Transform.rotate to rotate the Image based on the animation value
return Transform.rotate(
angle: _animation.value,
child: Image.asset(
'assets/1.png', // Replace with your image asset
width: 200,
height: 200,
),
);
},
),
),
);
}
@override
void dispose() {
// Dispose of the animation controller when the widget is disposed
_controller.dispose();
super.dispose();
}
}
Here is the full Code of main.dart file
Dart
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false,
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage>
with SingleTickerProviderStateMixin {
late AnimationController _controller;
late Animation<double> _animation;
@override
void initState() {
super.initState();
// Create an animation controller
_controller = AnimationController(
vsync: this, // vsync is set to this for performance reasons
duration: Duration(seconds: 2), // Set the duration of the animation
);
// Create a Tween for the rotation angle
_animation = Tween<double>(
begin: 0, // Start rotation angle
end: 2 * 3.141, // End rotation angle (2 * pi for a full circle)
).animate(_controller);
// Repeat the animation indefinitely
_controller.repeat();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Image Rotation Animation'),
),
body: Center(
child: AnimatedBuilder(
animation: _animation,
builder: (context, child) {
// Use Transform.rotate to rotate the Image based on the animation value
return Transform.rotate(
angle: _animation.value,
child: Image.asset(
'assets/1.png', // Replace with your image asset
width: 200,
height: 200,
),
);
},
),
),
);
}
@override
void dispose() {
// Dispose of the animation controller when the widget is disposed
_controller.dispose();
super.dispose();
}
}
Output:
Similar Reads
Flutter - Animated Navigation
By default, Flutter has no animation when navigating from one Screen to another Screen. But in this tutorial, we are going to learn how to animate the Screen Transition. Project Setup Before directly applying to your existing project, practice the code in some example projects. For this tutorial, we
4 min read
Flutter - Page Transition Animation
In Flutter, we can easily animate or handle the page transitions by using the page_transition package. The page_transition package is a valuable addition to the Flutter package, offering a variety of transition effects that can elevate the app's UI. In this article, we are going to explore how to in
6 min read
Flutter - Animation in Route Transition
Routes are simply Pages in Flutter applications. An application often needs to move from one page to another. However, animations can be used to make these transitions smoother. These animations can be used to curve or tween the Animation object of the PageRouteBuilder class to alter the transition
3 min read
Animated Text in Flutter
Animations make the UI more interactive and enhance the user experience. There is no limitation of creativity when it comes to animations or making the application more interactive. In Flutter, we got an easy way to display Animated text. In this article, we will see how we can animate texts using a
3 min read
Flutter - Animation Text Fade Transition
A fade text effect typically refers to a visual effect in which text gradually appears or disappears by changing its opacity over time, creating a smooth transition between visible and invisible states. In this article we are going to create a Text then we are going to add a Fading effect on it over
4 min read
Flutter - AnimatedIcon Widget
Animation has become a vital part of UI design, whether it be a transition from one window to another or the closing of the window, the animation plays an important role in the smooth representation of the process. Just like that when we click on the icon, it shows animation, or another icon is show
3 min read
Animated Widgets in Flutter
The animations are considered hard work and take time to learn. Flutter made it easy with its packages. To animate the widgets without much effort, we can wrap them inside different defined animated widgets in the animate_do package. In this article, we will see how with the animate_do package we ca
4 min read
Page View Animation in Flutter
Page View is a list that works page by page. In this article, we will gonna do How to Animate the Page when sliding. A sample video is given below to get an idea about what we are going to do in this article. We will use the Transform widget to animate the page. Syntax Creating a Page controller th
4 min read
Flutter - Lottie Animation
Visualization is an integral part of any application. Animations can highly glorify the UI of an app, but animations can be hectic to implement for an application. This is where the Lottie animation comes in. Lottie is a JSON-based animation file. It can be used both as a network asset and a static
3 min read
Flutter - Radial Hero Animation
A Radial transformation means turning a circular shape into a square shape. In Radial Hero animation the same is done with a Hero. In Flutter, a Hero Widget is used to create a hero animation. A hero in this context refers to a widget that moves in between screens. This is one of the most fundamenta
3 min read