How to get a value from a dialog in flutter?
What is missing is that you need to return the value when closing the dialog, like this:
return AlertDialog( title: Slider( value: value, min: 0, max: 100, onChanged: (va) { setState(() { value = va; }); }, ), actions: [ ElevatedButton( onPressed: () => Navigator.pop(context), child: Text('cancel')), ElevatedButton( onPressed: () => Navigator.pop(context, value), child: Text('confirm')), ],);
It will then be returned by the showDialog function as expected as a Future
You can access and use the value that comes from your dialog option like this:
showDialog( context: context, builder: (context) => Dialog( val: vale, ),).then((valueFromDialog){ // use the value as you wish print(valueFromDialog);});
The .then()
will be triggered after the user selects an option on your Dialog.