Angular vs. React: Which One Should You Choose?
Discover Serenity: Your Guide to Mindfulness Apps
Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps
Discover How Smartwatches Measure Blood Pressure: Explained Simply
13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!
Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo
Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!
Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals
Affordable VR Adventures: The Best Budget VR Headsets
Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying
Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More
Discover Exciting New Games on NVIDIA GeForce NOW!
Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games
The Future of iPhones: Apple's Bold Step into AI with DarwinAI
Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!
Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!
Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches
Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024
Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!
Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent
Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer
Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!
JavaScript Essentials: Your Quick Start to Web Coding
Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!
Gaming Joy Awaits: Explore the Best PS5 Games of 2024
Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!
Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit
Feel the Beat: Exploring Top Over-Ear Headphones
Explore the Web Development Strategies in 2024: A Developers Handbook
Explore Must-Play Nintendo Switch Games in 2024!
Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle
Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World
Discover Waze's cool new features for safer and happier drives!
Discover the Top Picks: Best Smartwatches for Your Lifestyle
Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!
Sound Revolution: Discover the Best Bluetooth Speakers of 2024!
Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks
Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024
Crack the Code: Your Guide to Computer Programming Magic in 2024
Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity
10 Best Tech Companies in Silicon Valley
Top 10 Web Development Interview Questions you can...
Learn how to Answer Tell me about Yourself
5 Books You Need to Read Right Now
25 Practical Ways to Earn Money Online
Translate this page in your preferred language:
Article by: Manish Methani
Last Updated: September 21, 2021 at 8:04am IST
Flutter is an open-source mobile application development framework created by Google. It allows you to build high-performance, cross-platform applications with a single codebase. In this tutorial, we will learn how to animate widget position in Flutter using the AnimatedPositioned widget.
Before we begin, make sure you have Flutter installed on your machine. If you don't, you can follow the installation guide provided in the official Flutter documentation.
Once you have Flutter installed, create a new Flutter project using the following command:
flutter create animated_positioned_demo
The AnimatedPositioned widget is a Flutter widget that animates its position within its parent widget. To use the AnimatedPositioned widget, we need to first create a container widget with a fixed size that will act as the parent widget for our AnimatedPositioned widget.
Here's an example of how to create a container widget with a fixed size:
Container( height: 200.0, width: 200.0, color: Colors.blue, child: // Child widgets will go here ),
Next, we need to create our AnimatedPositioned widget and add it as a child to our container widget. Here's an example of how to create an AnimatedPositioned widget:
AnimatedPositioned( duration: Duration(milliseconds: 500), curve: Curves.easeInOut, top: 0.0, left: 0.0, child: // Child widgets will go here ),
In the code above, we've created an AnimatedPositioned widget with a duration of 500 milliseconds and an ease-in-out curve. We've also set the initial position of the widget to the top-left corner of the parent container by setting the top
and left
properties to 0.0.
To animate the position of our AnimatedPositioned widget, we need to change the values of its top
and left
properties. We can do this by wrapping our AnimatedPositioned widget in an AnimatedBuilder
widget and updating the values of top
and left
in its builder function.
Here's an example of how to animate the position of our AnimatedPositioned widget:
class MyHomePage extends StatefulWidget { @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { bool _isPositionedRight = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Animated Widget Position'), ), body: Center( child: Stack( children: [ AnimatedPositioned( duration: Duration(seconds: 1), curve: Curves.easeInOut, left: _isPositionedRight ? 200.0 : 0.0, child: Container( width: 100.0, height: 100.0, color: Colors.blue, ), ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { _isPositionedRight = !_isPositionedRight; }); }, child: Icon(Icons.play_arrow), ), ); } }
In the code above, we've created an AnimatedBuilder
widget that takes an Animation
object as its argument.
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { bool _isPositionedRight = false; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Animated Widget Position'), ), body: Center( child: Stack( children: [ AnimatedPositioned( duration: Duration(seconds: 1), curve: Curves.easeInOut, left: _isPositionedRight ? 200.0 : 0.0, child: Container( width: 100.0, height: 100.0, color: Colors.blue, ), ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: () { setState(() { _isPositionedRight = !_isPositionedRight; }); }, child: Icon(Icons.play_arrow), ), ); } }
In this tutorial, we've learned how to animate widget position in Flutter using the AnimatedPositioned
widget. We've also learned how to wrap our AnimatedPositioned
widget in an AnimatedBuilder
widget to update its position using an animation.
By following the steps outlined in this tutorial, you should now be able to create animated widgets that change position within their parent widgets in your Flutter applications.
A: The AnimatedPositioned widget is a built-in Flutter widget that animates the position and size of its child widget.
A: To use the AnimatedPositioned widget, wrap the widget you want to animate with it and set the desired animation properties, such as duration and curve. Then, update the widgets position or size by setting the appropriate properties, such as left or top for position.
A: Yes, you can animate the position of any widget in Flutter by wrapping it with the AnimatedPositioned widget and updating its position properties.
A: You can trigger the animation of the widget position by changing the properties of the AnimatedPositioned widget, such as left or top, and Flutter will animate the position change automatically.
A: Yes, you can apply different animation curves to the widget position animation by setting the curve property of the AnimatedPositioned widget to a predefined Flutter animation curve, or create your own custom curve.
A: Yes, you can animate the position of multiple widgets simultaneously in Flutter by wrapping each widget with an AnimatedPositioned widget and setting their respective position properties. However, you will need to coordinate the animation properties to ensure that the widgets animate smoothly together.
Test your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test