Flutter - Toggle the Visibility of an Alert Dialog
Last Updated : 28 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