Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Types
  • Functions
  • Oops
  • Collections
  • Sets
  • Dart Interview Questions
  • Fluter
  • Android
  • Kotlin
  • Kotlin Android
  • Android with Java
  • Android Studio
Open In App
Next Article:
Flutter - Toggle the Visibility of an Alert Dialog
Next article icon

Flutter - Toggle the Visibility of an Alert Dialog

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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:


Next Article
Flutter - Toggle the Visibility of an Alert Dialog

C

chinmaya121221
Improve
Article Tags :
  • Dart
  • Flutter
  • Geeks Premier League
  • Flutter-Widgets
  • Geeks Premier League 2023

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences