Open In App

Flutter – Toggle the Visibility of an Alert Dialog

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
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:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads