Flutter - Toggle the Visibility of an Alert Dialog
Last Updated :
24 Apr, 2025
Alert Dialog is a very useful way to grab user interactions, here we use the Offstage widget to toggle (Hide/Show) the visibility of the Alert Dialog. The Offstage widget in Flutter is used to hide a widget from the user interface. It does this by laying out the child widget as if it was in the tree, but without painting anything, without making the child available for hit-testing, and without taking any room in the parent. In this article, we are going to implement the Offstage widget with the AlertDialog to Hide/Show the visibility of the Alert Dialog. A sample video is given below to get an idea about what we are going to do in this article.
Basic Syntax of Offstage Widget
Offstage(
offstage: true,
child: MyWidget(),
)
Basic Syntax of Alert Dialog Widget
AlertDialog(
title: Text('Dialog Title'), // The title of the dialog
content: Text('Dialog Content'), // The content of the dialog
actions: <Widget>[
TextButton(
onPressed: () {
// Add your action logic here
},
child: Text('Action 1'),
),
TextButton(
onPressed: () {
// Add your action logic here
},
child: Text('Action 2'),
),
],
)
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(
// Define the app's theme
theme: ThemeData(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false, // Disable debug banner
home: MyAlertDialogOffstage(),
);
}
}
Step 5: Create MyAlertDialogOffstage Class
In this class we are going to Implement the Offstage widget and the Alert Dialog Widget, When the user taps on the "Toggle AlertDialog" button, the toggleDialogVisibility method is called and it will toggle the boolean varriable value then accordingly the Visibility(Hide/ Show) of the Alert Dialog will be Set. Comments are added for better understanding.
Offstage(
offstage: !isDialogVisible,
child: AlertDialog(
//creating an Alert Dialog
title: Text('GFG'),
content: Text('Geeks Premier League 2023'),
actions: [
TextButton(
onPressed: () {
toggleDialogVisibility();
},
child: Text('Close'),
),
],
),
),
Dart
class _MyAlertDialogOffstageState extends State<MyAlertDialogOffstage> {
bool isDialogVisible = false;
// Funtion to toggle the Boolean value so that
// it can toogle the visibilty of Alert Dialog
void toggleDialogVisibility() {
setState(() {
isDialogVisible = !isDialogVisible;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Toggle AlertDialog Example'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: ElevatedButton(
onPressed: toggleDialogVisibility,
child: Text('Toggle AlertDialog'),
),
),
Offstage(
offstage: !isDialogVisible,
child: AlertDialog(
// creating an Alert Dialog
title: Text('GFG'),
content: Text('Geeks Premier League 2023'),
actions: [
TextButton(
onPressed: () {
toggleDialogVisibility();
},
child: Text('Close'),
),
],
),
),
],
),
);
}
}
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(
primarySwatch: Colors.green, // Set the app's primary theme color
),
debugShowCheckedModeBanner: false, // Disable debug banner
home: MyAlertDialogOffstage(),
);
}
}
class MyAlertDialogOffstage extends StatefulWidget {
@override
_MyAlertDialogOffstageState createState() => _MyAlertDialogOffstageState();
}
class _MyAlertDialogOffstageState extends State<MyAlertDialogOffstage> {
bool isDialogVisible = false;
// Funtion to toggle the Boolean value so that
// it can toogle the visibilty of Alert Dialog
void toggleDialogVisibility() {
setState(() {
isDialogVisible = !isDialogVisible;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Toggle AlertDialog Example'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: ElevatedButton(
onPressed: toggleDialogVisibility,
child: Text('Toggle AlertDialog'),
),
),
Offstage(
offstage: !isDialogVisible,
child: AlertDialog(
// creating an Alert Dialog
title: Text('GFG'),
content: Text('Geeks Premier League 2023'),
actions: [
TextButton(
onPressed: () {
toggleDialogVisibility();
},
child: Text('Close'),
),
],
),
),
],
),
);
}
}
Output:
Similar Reads
Flutter - Make a Toggle Password Visibility Button
In this article, we are going to create a button when pressed, displays the password entered in the obscured text field in Flutter. In this we are going to take a boolean variable named as showPassword, If showPassword is true, the password is displayed in plain text, otherwise, it is obscured. In t
4 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 be
2 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
4 min read
Flutter - Implement an Image Inside an AlertDialog
AlertDialogs are a common tool used to deliver messages, notifications, or interactive content to users. While Flutter provides a straightforward way to create AlertDialogs, it may not be immediately obvious how to include images inside them. Fortunately, this article will guide you through the proc
4 min read
Flutter - Creating Dialog in using GetX Library
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
3 min read
Flutter - Implement Stepper Widget Inside an AlertDialog
The Stepper widget in Flutter is a user interface element used to create a step-by-step process interface. It allows users to progress through a series of steps or stages, with each step typically representing a specific task or some information. An Alert Dialog is a useful way to grab users' attent
5 min read
How to Disable a Button in Flutter?
Sometimes we need to make the button disabled, let us consider, you created a form to take the user input and a form submit button. Then you want that, the button must be disabled until the user fills all the fields of the form then immediately enable the button. Sample Output is given below what we
3 min read
How to Dismiss the Virtual Keyboard in a Flutter App?
We all have faced situations after filling the text field the virtual keyboard does not go away automatically. We have to click on the back button of our smartphones to remove the virtual keyboard. So in this article, we will see how to dismiss the virtual keyboard in an app with just one tap. For t
2 min read
How to Build a Simple Login App with Flutter?
To prevent unauthorized access to personal information, it is a good practice to authenticate users before granting access to websites, mobile applications, and computer applications after they can register and then log in. In this article, we are going to explain how to build a Design Login page us
8 min read
Flutter - Display a GridView Within an AlertDialog Box
AlertDialogs are useful Widgets in Flutter to attract the users attention to a particular message that is showing by the Alert Dialogs, GridView are efficient widgets to display items in a Grid like structure. To implement a GridView inside an AlertDialog in Flutter, you can create a custom dialog c
5 min read