Open In App

Flutter – Animated AlertDialog in Flutter

Last Updated : 19 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Animating an AlertDialog in Flutter involves using the Flutter animations framework to create custom animations for showing and hiding the dialogue. In this article, we are going to add an animation to an AlertDialog. A sample video is given below to get an idea about what we are going to do in this article.

Basic Syntax of Creating an Animation in Flutter

Create an AnimationController:

final AnimationController _controller = AnimationController(
duration: Duration(seconds: 1), // Set the duration of the animation
vsync: this, // Provide a TickerProvider, often you can use 'this'
);

Define the Animation:

final Animation<double> _animation = Tween<double>(
begin: 0.0, // Starting value of the animation
end: 1.0, // Ending value of the animation
).animate(_controller);

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(
      // Define the app's theme
      theme: ThemeData(
        // Set the app's primary theme color
        primarySwatch: Colors.green, 
      ),
      debugShowCheckedModeBanner: false,
      home: OpenDialog(),
    );
  }
}


Step 5: Create OpenDialog Class

In this class we are going to create a button by clicking the button a animated AlertDialog will opened. Comments are added for better understanding.

Dart




class OpenDialog extends StatelessWidget {
  // Function to show the animated dialog
  void _showAnimatedDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AnimatedAlertDialog();
      },
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Animated AlertDialog Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            _showAnimatedDialog(context);
          },
          child: Text('Show Animated Dialog'),
        ),
      ),
    );
  }
}


Step 6: Create AnimatedAlertDialog Class

In this class we are going to add animation to an AlertDialog .It define a AnimationController then we use Tween animation for it. Comments are added for better understanding .

Dart




class AnimatedAlertDialog extends StatefulWidget {
  @override
  _AnimatedAlertDialogState createState() => _AnimatedAlertDialogState();
}
  
class _AnimatedAlertDialogState extends State<AnimatedAlertDialog>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _scaleAnimation;
  
  @override
  void initState() {
    super.initState();
  
    _controller = AnimationController(
      vsync: this,
      // Adjust the duration as needed
      duration: Duration(milliseconds: 800), 
    );
  
    _scaleAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller);
  
    // Start the animation 
    // when the dialog is displayed
    _controller.forward();
  }
  
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ScaleTransition(
        scale: _scaleAnimation,
        child: AlertDialog(
          title: Text("Animated Alert Dialog"),
          content: Text("This is an animated AlertDialog."),
          actions: <Widget>[
            TextButton(
              child: Text("Close"),
              onPressed: () {
                // Reverse the animation and close the dialog
                _controller.reverse();
                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      ),
    );
  }
}


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(
      // Define the app's theme
      theme: ThemeData(
        // Set the app's primary theme color
        primarySwatch: Colors.green, 
      ),
      debugShowCheckedModeBanner: false,
      home: OpenDialog(),
    );
  }
}
  
class OpenDialog extends StatelessWidget {
  // Function to show the animated dialog
  void _showAnimatedDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AnimatedAlertDialog();
      },
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Animated AlertDialog Example'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            _showAnimatedDialog(context);
          },
          child: Text('Show Animated Dialog'),
        ),
      ),
    );
  }
}
  
class AnimatedAlertDialog extends StatefulWidget {
  @override
  _AnimatedAlertDialogState createState() => _AnimatedAlertDialogState();
}
  
class _AnimatedAlertDialogState extends State<AnimatedAlertDialog>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _scaleAnimation;
  
  @override
  void initState() {
    super.initState();
  
    _controller = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 800), // Adjust the duration as needed
    );
  
    _scaleAnimation = Tween<double>(begin: 0.0, end: 1.0).animate(_controller);
  
    // Start the animation when 
    // the dialog is displayed
    _controller.forward();
  }
  
  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ScaleTransition(
        scale: _scaleAnimation,
        child: AlertDialog(
          title: Text("Animated Alert Dialog"),
          content: Text("This is an animated AlertDialog."),
          actions: <Widget>[
            TextButton(
              child: Text("Close"),
              onPressed: () {
                // Reverse the animation 
                // and close the dialog
                _controller.reverse();
                Navigator.of(context).pop();
              },
            ),
          ],
        ),
      ),
    );
  }
}


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads