Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data Types
  • Functions
  • Oops
  • Collections
  • Sets
  • Dart Interview Questions
  • Fluter
  • Android
  • Kotlin
  • Kotlin Android
  • Android with Java
  • Android Studio
Open In App
Next Article:
Flutter - AnimatedPositioned Widget
Next article icon

Flutter - Create Animation Using the AnimatedAlign Widget

Last Updated : 16 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The AnimatedAlign widget in Flutter is used to create animated transitions for aligning a child widget within a parent container. It smoothly animates the alignment property, allowing you to move a child widget from one position to another with a specified duration. In this article, we are going to implement the AnimatedAlign widget. A sample video is given below to get an idea about what we are going to do in this article.

Basic Syntax of AnimatedAlign Widget

Dart
AnimatedAlign(   duration: Duration(milliseconds: 500), // Duration for the animation   alignment: Alignment.center, // Alignment of the child widget   curve: Curves.easeInOut, // Animation curve (optional)   child: YourChildWidget(), // The widget you want to animate ) 

Required Tools

To build this app, you need the following items installed on your machine:

  • Visual Studio Code / Android Studio
  • Android Emulator / iOS Simulator / Physical Device device.
  • Flutter Installed
  • Flutter plugin for VS Code / Android Studio.

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: AlignAnimationExample(),     );   } } 

Step 5: Create AlignAnimationExample Class

In this class we are going to Implement the AlignAnimation widget that help to create animated transitions for aligning a child text widget within a parent container ,In this class we are going to align the container widget left to right and right to left.Comments are added for better understanding.

AnimatedAlign(
duration: Duration(seconds: 1), // Animation duration
alignment: _isAlignedRight
? Alignment.centerRight
: Alignment.centerLeft, // Toggle alignment
child: Container(
width: 100,
height: 100,
color: Colors.green, // Background color of the container
child: Center(
child: Text(
'Hello',
style: TextStyle(color: Colors.white), // Text color
),
),
),
),
Dart
class AlignAnimationExample extends StatefulWidget {   @override   _AlignAnimationExampleState createState() => _AlignAnimationExampleState(); }  class _AlignAnimationExampleState extends State<AlignAnimationExample> {   bool _isAlignedRight = false;    void _toggleAlignment() {     setState(() {       _isAlignedRight = !_isAlignedRight; // Toggle the alignment state     });   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text('AnimatedAlign Demo'), // App bar title       ),       body: Center(         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             AnimatedAlign(               duration: Duration(seconds: 1), // Animation duration               alignment: _isAlignedRight                   ? Alignment.centerRight                   : Alignment.centerLeft, // Toggle alignment               child: Container(                 width: 100,                 height: 100,                 color: Colors.green, // Background color of the container                 child: Center(                   child: Text(                     'Hello',                     style: TextStyle(color: Colors.white), // Text color                   ),                 ),               ),             ),             SizedBox(height: 20), // Empty space between elements             ElevatedButton(               onPressed: _toggleAlignment, // Button click handler               child: Text(_isAlignedRight                   ? 'Align Left'                   : 'Align Right'), // Button text             ),           ],         ),       ),     );   } } 

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: AlignAnimationExample(),     );   } }  class AlignAnimationExample extends StatefulWidget {   @override   _AlignAnimationExampleState createState() => _AlignAnimationExampleState(); }  class _AlignAnimationExampleState extends State<AlignAnimationExample> {   bool _isAlignedRight = false;    void _toggleAlignment() {     setState(() {       _isAlignedRight = !_isAlignedRight; // Toggle the alignment state     });   }    @override   Widget build(BuildContext context) {     return Scaffold(       appBar: AppBar(         title: Text('AnimatedAlign Demo'), // App bar title       ),       body: Center(         child: Column(           mainAxisAlignment: MainAxisAlignment.center,           children: <Widget>[             AnimatedAlign(               duration: Duration(seconds: 1), // Animation duration               alignment: _isAlignedRight                   ? Alignment.centerRight                   : Alignment.centerLeft, // Toggle alignment               child: Container(                 width: 100,                 height: 100,                 color: Colors.green, // Background color of the container                 child: Center(                   child: Text(                     'Hello',                     style: TextStyle(color: Colors.white), // Text color                   ),                 ),               ),             ),             SizedBox(height: 20), // Empty space between elements             ElevatedButton(               onPressed: _toggleAlignment, // Button click handler               child: Text(_isAlignedRight                   ? 'Align Left'                   : 'Align Right'), // Button text             ),           ],         ),       ),     );   } } 

Output:



Next Article
Flutter - AnimatedPositioned Widget
author
chinmaya121221
Improve
Article Tags :
  • Dart
  • Flutter
  • Geeks Premier League
  • Flutter-Widgets
  • Geeks Premier League 2023

Similar Reads

  • Flutter - AnimatedPositioned Widget
    The AnimatedPositioned widget in Flutter is used to create animated transitions for a widget's position within a Stack. It allows you to smoothly change the position of a child widget by animating the values of the left, top, right, and bottom properties. In this article, we are going to implement t
    4 min read
  • Flutter - Create Animated Pie Chart
    Pie charts are an effective way to represent data in a visually appealing and easily understandable format. In Flutter, creating a pie chart is relatively easy with the help of a few external packages. In this article, we will explore how to create a pie chart in Flutter and how to animate it. Use c
    4 min read
  • Creating a Calculator using Calculator Widget in Flutter
    If you need a Calculator in Flutter or need to do a small calculation in your flutter application, Writing your own code going to be tough, but Flutter gives you a SimpleCalculator Widget, Which allows you to create a Beautiful simple calculator. Only you need to set up the package in pubspec.yaml f
    3 min read
  • Flutter - AnimatedContainer Widget
    In Flutter a container is a simple widget with well-defined properties like height, width, and color, etc. The AnimatedContainer widget is a simple container widget with animations. These types of widgets can be animated by altering the values of their properties which are the same as the Container
    3 min read
  • Flutter - Animate Items in List Using AnimatedList
    The AnimatedList widget in Flutter is used to create a list that automatically animates changes to its items. It's particularly useful when you want to add or remove items from a list with smooth animations. In this article, we are going to see how the list is animated when an item is deleted or add
    5 min read
  • Flutter - Loading Animation Widget
    In every mobile application, there is a loading animation with different colors and styles, basically, we use the loading animation when we are waiting for something. Like if we are fetching the data from the database then we have to wait for some time until the data is not fetched. So in this durat
    3 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
  • Flutter - AnimatedSwitcher Widget
    The AnimatedSwitcher widget in Flutter is used to animate the transition between two or more widgets with a smooth animation. It's often used when you want to switch the display of different widgets within the same space and provide a visual transition effect between them. In this article, we are go
    4 min read
  • Flutter - AnimatedCrossFade Widget
    AnimatedCrossFade Widget creates a fade transition between two widgets when one widget is replaced by another. It is used when you need to give a fade kind of transition in between two widgets. It supports any kind of Flutter Widget like Text, Images, Icon as well as anything that is extended from t
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences