Flutter - AnimatedPhysicalModel Widget
Last Updated :
24 Apr, 2025
In Flutter, the AnimatedPhysicalModel widget is used to create animations that transition between different physical properties of a widget, such as elevation, shape, and colour. In this article, we are going to implement the AnimatedPhysicalModel widget. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of AnimatedPhysicalModel
AnimatedPhysicalModel(
duration: Duration(milliseconds: 500), // Animation duration
curve: Curves.easeInOut, // Animation curve
elevation: _elevation, // Elevation (animate this property)
shape: BoxShape.rectangle, // Shape of the widget (rectangle or circle)
shadowColor: Colors.grey, // Color of the shadow
color: Colors.blue, // Background color of the widget
borderRadius: BorderRadius.circular(8.0), // Border radius
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: AnimatedPhysicalModelDemo(),
);
}
}
Step 5: Create AnimatedPhysicalModelDemo Class
In this class we are going to Implement the AnimatedPhysicalModel widget that help to creating an animated card like container that changes its elevation, shape, and color when the "Add Elevation" or "Remove Elevation" button is pressed. Comments are added for better understanding.
AnimatedPhysicalModel(
duration: Duration(seconds: 1), // Animation duration
curve: Curves.easeInOut, // Animation curve
elevation: _isElevated ? 68.0 : 0.0, // Animate elevation
shape: BoxShape.rectangle, // Shape of the container
shadowColor: Colors.green, // Color of the shadow
color: Colors.green, // Color of the container
borderRadius: BorderRadius.circular(8.0), // Border radius
child: Container(
width: 200,
height: 200,
child: Center(
child: Text(
'Animated',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
),
),
Dart
class AnimatedPhysicalModelDemo extends StatefulWidget {
@override
_AnimatedPhysicalModelDemoState createState() =>
_AnimatedPhysicalModelDemoState();
}
class _AnimatedPhysicalModelDemoState extends State<AnimatedPhysicalModelDemo> {
bool _isElevated = false;
void _toggleElevation() {
setState(() {
_isElevated = !_isElevated; // Toggle elevation state
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AnimatedPhysicalModel Demo'), // App bar title
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedPhysicalModel(
duration: Duration(seconds: 1), // Animation duration
curve: Curves.easeInOut, // Animation curve
elevation: _isElevated ? 68.0 : 0.0, // Animate elevation
shape: BoxShape.rectangle, // Shape of the container
shadowColor: Colors.green, // Color of the shadow
color: Colors.green, // Color of the container
borderRadius: BorderRadius.circular(8.0), // Border radius
child: Container(
width: 200,
height: 200,
child: Center(
child: Text(
'Animated',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
),
),
SizedBox(height: 20), // Empty space between elements
ElevatedButton(
onPressed: _toggleElevation, // Button click handler
child: Text(_isElevated
? 'Remove Elevation'
: 'Add Elevation'), // 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: AnimatedPhysicalModelDemo(),
);
}
}
class AnimatedPhysicalModelDemo extends StatefulWidget {
@override
_AnimatedPhysicalModelDemoState createState() =>
_AnimatedPhysicalModelDemoState();
}
class _AnimatedPhysicalModelDemoState extends State<AnimatedPhysicalModelDemo> {
bool _isElevated = false;
void _toggleElevation() {
setState(() {
_isElevated = !_isElevated; // Toggle elevation state
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('AnimatedPhysicalModel Demo'), // App bar title
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedPhysicalModel(
duration: Duration(seconds: 1), // Animation duration
curve: Curves.easeInOut, // Animation curve
elevation: _isElevated ? 68.0 : 0.0, // Animate elevation
shape: BoxShape.rectangle, // Shape of the container
shadowColor: Colors.green, // Color of the shadow
color: Colors.green, // Color of the container
borderRadius: BorderRadius.circular(8.0), // Border radius
child: Container(
width: 200,
height: 200,
child: Center(
child: Text(
'Animated',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
),
),
SizedBox(height: 20), // Empty space between elements
ElevatedButton(
onPressed: _toggleElevation, // Button click handler
child: Text(_isElevated
? 'Remove Elevation'
: 'Add Elevation'), // Button text
),
],
),
),
);
}
}
Output:
Similar Reads
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
Flutter - AnimatedBuilder Widget
The AnimatedBuilder widget in Flutter is a powerful utility widget that is used for creating complex animations by rebuilding a part of the widget tree in response to changes in an animation's value. It is especially useful when you want to animate properties of child widgets that cannot be directly
4 min read
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 - 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
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
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
AnimatedSize Flutter
In mobile app development, animations and transitions play a major role in providing a simple and smooth user experience, making it visually appealing to the users, interacting with the app. What is AnimatedSize in Flutter?In Flutter, the AnimatedSize widget is a powerful tool, that automatically an
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 - Hinge Animation
Animations are a big part of the Flutter application. It makes an app sweet to the eyes and equally user-friendly. In this article, we will discuss in detail the Hinge animations. In Flutter there are two ways to work with animations namely: A pub packageAnimated Builder WidgetIn this article, we wi
4 min read