¿Cómo actualizar el widget principal después de cerrar showDialog en Flutter?
Nov 29 2020
Tengo un widget con un icono cuando hago clic en él, se muestra un widget de diálogo, aquí está la llamada al diálogo:
// this icon is in widget parent
IconButton(
icon: Icon(
Icons.info_outline,
size: mobileWidth * 0.07,
),
tooltip: 'information',
color: Colors.blueGrey,
onPressed: () {
showAlertInfo(context, code);
setState(() {});
},
),
aquí está mi diálogo:
showAlertInfo(BuildContext context, String code) {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text(
"Information sur le client $code", ),
content: SingleChildScrollView(
child: Container( .....
/* ...
this dialog has some operations that changes info
values of the widget that called this in first place
it is a big code to put here*/
// here I have a close button
actions: [
FlatButton(
child: Text(
"Fermer",
style: TextStyle(
color: Colors.red,
fontSize: mobileWidth * 0.035,
fontWeight: FontWeight.bold,
),
),
onPressed: () {
Navigator.of(context).pop(); // dismiss dialog
},
),
],
);
lo que estoy tratando de lograr es que cuando se descarte el cuadro de diálogo quiero que se actualice el widget principal, entonces, ¿cómo llamo setState
al widget principal cuando el widget de diálogo está cerrado?
Respuestas
2 TahaMalik Nov 29 2020 at 06:58
Para la función Async
Agregue await
antes showDialog()
y llame setState((){})
en la siguiente línea.
await showDialog(
context: context,
builder: (context) {
return yourWidget;
}
);
setState((){});
Para la función de sincronización
Utilice la .then()
devolución de llamada y llámela setState((){})
.
showDialog(
context: context,
builder(context) {
return yourWidget;
}).then((_){
setState((){});
}
);