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), ),
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.
Simply use await or then . the code in then block will be run after the dialog is dismissed.
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"), ), ], ); }, ); }, );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With