Open In App

Flutter – Animate Image Rotation

Last Updated : 14 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads