![]() |
VOOZH | about |
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.
Offstage(
offstage: true,
child: MyWidget(),
)
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'),
),
],
)
To build this app, you need the following items installed on your machine:
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.
First of all import material.dart file.
import 'package:flutter/material.dart';
Here the execution of our app starts.
In this class we are going to implement the MaterialApp , here we are also set the Theme of our App.
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'),
),
],
),
),