Flutter – AnimatedPhysicalModel Widget
Last Updated :
24 Oct, 2023
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,
),
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;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'AnimatedPhysicalModel Demo' ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedPhysicalModel(
duration: Duration(seconds: 1),
curve: Curves.easeInOut,
elevation: _isElevated ? 68.0 : 0.0,
shape: BoxShape.rectangle,
shadowColor: Colors.green,
color: Colors.green,
borderRadius: BorderRadius.circular(8.0),
child: Container(
width: 200,
height: 200,
child: Center(
child: Text(
'Animated' ,
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _toggleElevation,
child: Text(_isElevated
? 'Remove Elevation'
: 'Add Elevation' ),
),
],
),
),
);
}
}
|
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,
),
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;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'AnimatedPhysicalModel Demo' ),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
AnimatedPhysicalModel(
duration: Duration(seconds: 1),
curve: Curves.easeInOut,
elevation: _isElevated ? 68.0 : 0.0,
shape: BoxShape.rectangle,
shadowColor: Colors.green,
color: Colors.green,
borderRadius: BorderRadius.circular(8.0),
child: Container(
width: 200,
height: 200,
child: Center(
child: Text(
'Animated' ,
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _toggleElevation,
child: Text(_isElevated
? 'Remove Elevation'
: 'Add Elevation' ),
),
],
),
),
);
}
}
|
Output:
Share your thoughts in the comments
Please Login to comment...