The Wayback Machine - https://web.archive.org/web/20240613021508/https://www.geeksforgeeks.org/flutter-dialogs/
Open In App

Flutter – Dialogs

Last Updated : 20 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The dialog is a type of widget which comes on the window or the screen which contains any critical information or can ask for any decision. When a dialog box is popped up all the other functions get disabled until you close the dialog box or provide an answer. We use a dialog box for a different type of condition such as an alert notification, or simple notification in which different options are shown, or we can also make a dialog box that can be used as a tab for showing the dialog box.

Types of dialogs in a flutter

  • AlertDialog
  • SimpleDialog
  • showDialog

AlertDialog

Alert dialog tells the user about any condition that requires any recognition. The alert dialog contains an optional title and an optional list of actions.  We have different no of actions as our requirements. Sometimes the content is too large compared to the screen size so for resolving this problem we may have to use the expanded class.

Properties:

  • Title: It is always recommended to make our dialog title as short as possible. It will be easily understandable to the user.
  • Action: It is used to show the content for what action has to perform.
  • Content: The body of the alertDialog widget is defined by the content.
  • Shape: It is used to define the shape of our dialog box whether it is circular, curved, and many more.

Constructor:

AlertDialog(
               {
                   Key key,
                   Widget title,
                   EdgeInsetsGeometry titlePadding,
                   TextStyle titleTextStyle,
                   Widget content,
                   EdgeInsetsGeometry contentPadding: const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
                   TextStyle contentTextStyle,
                   List<Widget> actions,
                   EdgeInsetsGeometry actionsPadding: EdgeInsets.zero,
                   VerticalDirection actionsOverflowDirection,
                   double actionsOverflowButtonSpacing,
                   EdgeInsetsGeometry buttonPadding,
                   Color backgroundColor,
                   double elevation,
                   String semanticLabel,
                   EdgeInsets insetPadding: _defaultInsetPadding,
                   Clip clipBehavior: Clip.none,
                   ShapeBorder shape,
                   bool scrollable: false
             }
)

Here is the snippet code for creating a dialog box.

Dart




AlertDialog(
              title: Text('Welcome'),           // To display the title it is optional
              content: Text('GeeksforGeeks'),   // Message which will be pop up on the screen
                                                  // Action widget which will provide the user to acknowledge the choice
                actions: [
                FlatButton(                     // FlatButton widget is used to make a text to work like a button
                  textColor: Colors.black,
                  onPressed: () {},             // function used to perform after pressing the button
                  child: Text('CANCEL'),
                ),
                FlatButton(
                  textColor: Colors.black,
                  onPressed: () {},
                  child: Text('ACCEPT'),
                ),
              ],
            ),


Output:

Alert dialog

Alert dialog 

SimpleDialog 

A simple dialog allows the user to choose from different choices. It contains the title which is optional and presented above the choices. We can show options by using the padding also. Padding is used to make a widget more flexible.  

Properties:

  • Title: It is always recommended to make our dialog title as short as possible. It will be easily understandable to the user.
  • Shape: It is used to define the shape of our dialog box whether it is circular, curve, and many more.
  • backgroundcolor: It is used to set the background color of our dialog box.
  • TextStyle: It is used to change the style of our text.

Constructor:

const SimpleDialog(
     {
               Key key,
               Widget title,
               EdgeInsetsGeometry titlePadding: const EdgeInsets.fromLTRB(24.0, 24.0, 24.0, 0.0),
               TextStyle titleTextStyle,
               List<Widget> children,
               EdgeInsetsGeometry contentPadding: const EdgeInsets.fromLTRB(0.0, 12.0, 0.0, 16.0),
               Color backgroundColor,
               double elevation,
               String semanticLabel,
               ShapeBorder shape
     }
)

Here is the snippet code for Simple dialog 

Dart




SimpleDialog(
              title:const Text('GeeksforGeeks'),
              children: <Widget>[
                 SimpleDialogOption(
                       onPressed: () { },
                            child:const Text('Option 1'),
                         ),
                  SimpleDialogOption(
                     onPressed: () { },
                  child: const Text('Option 2'),
              ),
       ],
  ),


Output: 

SimpleDialog

SimpleDialog

showDialog 

It basically used to change the current screen of our app to show the dialog popup. You must call before the dialog popup. It exits the current animation and presents a new screen animation. We use this dialog box when we want to show a tab that will popup any type of dialog box, or we create a front tab to show the background process. 

Properties:

  • Builder: It returns the child instead of creating a child argument.
  • Barriercolor: It defines the modal barrier color which darkens everything in the dialog.
  • useSafeArea: It makes sure that the dialog uses the safe area of the screen only not overlapping the screen area.

Constructor:

Future<T> showDialog <T>(
    {
           @required BuildContext context,
           WidgetBuilder builder,
           bool barrierDismissible: true,
           Color barrierColor,
           bool useSafeArea: true,
           bool useRootNavigator: true,
           RouteSettings routeSettings,
          @Deprecated(It returns the child from the closure provided to the builder class ) Widget child
     }
)

Here is the snippet for the showDialog function.

Dart




showDialog(
           context: context,
           builder: (BuildContext context) {
                    return Expanded(
                      child: AlertDialog(
                        title: Text('Welcome'),
                        content: Text('GeeksforGeeks'),
                        actions: [
                          FlatButton(
                            textColor: Colors.black,
                            onPressed: () {},
                            child: Text('CANCEL'),
                          ),
                          FlatButton(
                            textColor: Colors.black,
                            onPressed: () {},
                            child: Text('ACCEPT'),
                          ),
                     ],
                ),
          );
       },
  );


Output: 

Key Difference

AlertDialog

  • Used to show any type of alert notification.
  • We can give the option to react to the alert popup like the accept button or reject button.

SimpleDialog

  • Used to show simple options as a dialog box.
  • There are different options to choose from and perform functions according to them. The option can be simple like choosing between different emails.

ShowDialog

  • Used to create an option that will popup a dialog box.
  • By using this we can popup different types of dialog boxes like alertDialog, and SimpleDialog as a sub widget.


Previous Article
Next Article

Similar Reads

Flutter - Giffy Dialogs
Giffy Dialogs is a highly customizable alert dialog box. It is implemented through the use of giffy_dialog package from flutter. This package is entirely written in Dart language and provides a wide range of features. There are three types of giffy dialogs listed below: Network giffy dialogFlare giffy dialogAsset giffy dialog Let's talk about them
5 min read
Is Flutter Worth Learning? Top 7 Reasons to Learn Flutter
In today's era, the usage of smartphones has increased exponentially and so has the use of mobile applications and websites. Meanwhile, considering future career prospects, learning web development and mobile app development is strongly recommended for all individuals. And when we come to mobile app development, there are two most-popular open-sour
5 min read
Flutter - Sharing Data Among Flutter Pages
In this article, we are going to find the solution for the problem statement "Import/send data from one page to another in flutter". Before going into the topic, there are some pre-requisites. Pre-requisites:Basics of dart programming language.Setting up Flutter in VS code or Setting up Flutter in Android Studio.Creating first flutter app | Hello W
4 min read
Flutter Quill - Rich Text Editor in Flutter Application
This post will explain how to integrate a rich text editor into your Flutter application so that it may enable rich text editing. There are many more things we can do with Flutter Quill, but they can get a little involved. The example below is a pretty basic demonstration of what we mean. Let's first discuss what flutter quill is and why we need it
5 min read
Flutter - Animated AlertDialog in Flutter
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. [video mp4="https://media.geeksforgeeks.
4 min read
Flutter - Different Types of Loading using Flutter EasyLoading
Flutter applications involve incorporating loading indicators to provide users with feedback during background operations. Flutter has a package for simplifying the process named flutter_easyloading package. This package easiest way to integrate loading indicators with various styles. In this article, we will implement different types of loading in
5 min read
Expandable Widget in Flutter
The expandable widgets do not have a fixed size, they expand on the screen according to the available areas on the screen. Due to size constraints, certain widgets can throw rendering errors, so widgets are made expandable. The one use of expandable widgets is cards that include images, texts, and to further read the text we can click "Read More" o
3 min read
Flutter - Difference Between ListView and List
Flutter is Google’s Mobile SDK to build native iOS and Android, Desktop (Windows, Linux, macOS), and Web apps from a single codebase. When building applications with Flutter everything is towards Widgets – the blocks with which the flutter apps are built. They are structural elements that ship with a bunch of material design-specific functionalitie
4 min read
Flutter - Mark as Favorite Feature
Adding to favorites is a prevalent feature in many applications. It enables the users to mark or save images, addressed, links or others stuff for easy future reference. In this article, we are going to see how to implement favorites or add to favorites feature in a flutter application. This article list two methods to do so. In the first method, w
9 min read
Flutter and Blockchain - Hello World Dapp
This tutorial will take you through the process of building your first mobile dapp - Hello World Dapp! This tutorial is meant for those with a basic knowledge of Ethereum and smart contracts, who have some knowledge of the Flutter framework but are new to mobile dapps. In this tutorial we will be covering: Setting up the development environmentCrea
9 min read