Flutter – Creating Dialog in using GetX Library
Last Updated :
14 May, 2021
When we want to show anything in the form of the dialog then we can create this Dialog using the GetX library in Flutter. When we normally create a dialog in flutter then it uses context and builder to create a Dialog. This is not a good practice for a developer to create Dialogs using contexts and builders. To overcome this problem, we can create Dialog using GetX with simple code and very easy to create a dialog. It does not use context and builder to create Dialog.
Follow the steps to create a Dialog in flutter using the GetX library:
- Create a new Flutter app:
flutter create APP_NAME
- Add get under dependencies in pubspec.yaml file:

import 'package:get/get.dart';
The constructor of Get.defaultDialog():
defaultDialog<T>({
String title = "Alert",
TextStyle? titleStyle,
Widget? content,
void Function()? onConfirm,
void Function()? onCancel,
void Function()? onCustom,
Color? cancelTextColor,
Color? confirmTextColor,
String? textConfirm,
String? textCancel,
String? textCustom,
Widget? confirm,
Widget? cancel,
Widget? custom,
Color? backgroundColor,
bool barrierDismissible = true,
Color? buttonColor,
String middleText = "Dialog made in 3 lines of code",
TextStyle? middleTextStyle,
double radius = 20.0,
List<Widget>? actions,
Future<bool> Function()? onWillPop})
Let’s discuss some properties of Get.defaultDialog():
- title: The title of the Dialog. By default, the title is “Alert”.
- titleStyle: The style given to the title text using TextStyle.
- content: The content given to the Dialog and should use Widget to give content.
- middleText: The middle text given to the Dialog. If we use content also then content widget data will be displayed.
- barrierDismissible: If we want to close the Dialog by clicking outside the dialog then its value should be true else false. By default, its value is true.
- middleTextStyle: The style given to the middle text using TextStyle.
- radius: The radius of the Dialog provided. By default, its value is 20.
- backgroundColor: The background color of the Dialog.
The main.dart file:
Dart
import 'package:flutter/material.dart' ;
import 'package:get/get.dart' ;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
title: 'Dialog Demo' ,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Home(),
debugShowCheckedModeBanner: false ,
);
}
}
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text( 'GeeksforGeeks Dialog' ),
centerTitle: true ,
backgroundColor: Colors.green,
),
body: Center(
child: ElevatedButton(
child: Text( 'Show Dialog' ),
onPressed: (){
Get.defaultDialog(
);
},
)
),
);
}
}
|
Explanation:
- The first step is to create an app and run it.
- We have used GetMaterialApp instead of MaterialApp because we are building our app using GetX library. If we do not use GetMaterialApp then its functionalities will not work.
- Then we have created a Home class that is Stateless. Then we have created a Scaffold widget.
- In the body, create a button in the center.
- Create dialog using Get.defaultDialog() .
- We can add extra features to this default dialog like backgroundColor, the radius of dialog, middle text, barrierDismissible, etc.
Output:
Get.defaultDialog(
title: "GeeksforGeeks",
middleText: "Hello world!",
backgroundColor: Colors.green,
titleStyle: TextStyle(color: Colors.white),
middleTextStyle: TextStyle(color: Colors.white),
);
When the above code is executed, the output will be:

Get.defaultDialog(
title: "GeeksforGeeks",
middleText: "Hello world!",
backgroundColor: Colors.green,
titleStyle: TextStyle(color: Colors.white),
middleTextStyle: TextStyle(color: Colors.white),
textConfirm: "Confirm",
textCancel: "Cancel",
cancelTextColor: Colors.white,
confirmTextColor: Colors.white,
buttonColor: Colors.red,
barrierDismissible: false,
radius: 50,
content: Column(
children: [
Container(child:Text("Hello 1")),
Container(child:Text("Hello 2")),
Container(child:Text("Hello 3")),
],
)
);
When we execute the above code, the output will be:

Similar Reads
Flutter - Creating Snackbar Using GetX Library
Sometimes, it's very useful to show the message when a certain action takes place in our app. Let's suppose, we have a list of items and we want to delete any item from the list then after deleting those items, some message should be displayed to inform the user that the item has been deleted. If we
2 min read
Flutter - Creating Bottomsheet GetX Library
Bottomsheets are the sheets displayed at the bottom to show any content that we want to display. Normally, when we create a bottomsheet, the syntax for creating it is long, and it also uses context. To avoid this, we can create a bottom sheet with simple code with the help of the GetX library. We ca
5 min read
Flutter - Creating Time Picker Using Material Design
Time Picker is a UI component that controls or lets the selection of time in either 24-hours format or AM/PM format. Mainly is job is to ease out the process of time selection and to ensure that the user is picking or selecting a valid time in the application. This can be used in the applications su
7 min read
Flutter - Flashbar, Toast and Dialog using Flash
The flutter community is making Flutter strong and popular through its libraries making the development faster and simpler. Flash is a Flutter library that serves multiple purposes. One can create Snackbars, toasts, and dialogs with it. Flutter does not have Toast by default. So, using Flash library
10 min read
Flutter - Creating App Intro Screens
Flutter is known for its easiness to create cross-platform applications. Either it is creating introduction screens for the app or any screen. We got an easy way to create Intros for the app using the intro_slider library of Flutter. In this article, we are going to implement it in the sample app. I
4 min read
Autofill Hints Suggestion List in Flutter
You must have noticed that the majority of websites and mobile applications provide an autofill list. These features can now be added to your Flutter app without importing any packages. Just read the article for this amazing property in the text form field/text field. Example: We need to display sev
2 min read
Flutter - Passing Multiple Data with GetX
GetX is a state management library for Flutter that provides a simple and powerful way to manage the state of your application. It provides various features like State management, Routing etc. Passing back the Data from the Second Screen to the First Screen involves State Management to achieve this
5 min read
Getting Started with Cross-Platform Mobile Application using Flutter
Flutter is an open-source mobile application development SDK created by Google to develop cross-platform mobile applications. Flutter makes it extremely easy and fast for even novice programmers (computer programmers who are not experienced in any programming languages) to build high-quality and res
7 min read
Flutter - Create a Dart Model using Freezed Package
In this article, we will learn how to create a dart model automatically with a sample of code using the freezed package. If you are making a large-scale app. You cannot write a model class file for every data you receive from the backend. So here's a simple solution for it where this model will auto
3 min read
Alert Dialog box in Flutter
Alert Dialog box informs the user about the situation that requires acknowledgment. Alert Box is a prompt that takes user confirmation. The very basic use of the alert box is used when we close the app, usually, we are notified with a prompt whether we want to exit or not. That's an alert box. The b
2 min read