Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh an AlertDialog in Flutter?

Currently, I have an AlertDialog with an IconButton. The user can click on the IconButton, I have two colors for each click. The problem is that I need to close the AlertDialog and reopen to see the state change of the color icon. I want to change the IconButton color immediately when the user clicks it.

Here is the code:

bool pressphone = false; //.... new IconButton(    icon: new Icon(Icons.phone),    color: pressphone ? Colors.grey : Colors.green,    onPressed: () => setState(() => pressphone = !pressphone), ), 
like image 577
Nitneuq Avatar asked Aug 22 '18 07:08

Nitneuq


People also ask

How do I return AlertDialog in Flutter?

Create a Flutter project in Android Studio and replace the following code with main. dart file. To show an alert, you must have to call showDialog() function, which contains the context and itemBuilder function. The itemBuilder function returns an object of type dialog, the AlertDialog.

How do I run code after showDialog is dismissed in Flutter?

Simply use await or then . the code in then block will be run after the dialog is dismissed.


1 Answers

Use StatefulBuilder to use setState inside Dialog and update Widgets only inside of it.

showDialog(   context: context,   builder: (context) {     String contentText = "Content of Dialog";     return StatefulBuilder(       builder: (context, setState) {         return AlertDialog(           title: Text("Title of Dialog"),           content: Text(contentText),           actions: <Widget>[             TextButton(               onPressed: () => Navigator.pop(context),               child: Text("Cancel"),             ),             TextButton(               onPressed: () {                 setState(() {                   contentText = "Changed Content of Dialog";                 });               },               child: Text("Change"),             ),           ],         );       },     );   }, ); 
like image 106
Mehmet Ali Bayram Avatar answered Oct 27 '22 05:10

Mehmet Ali Bayram