I am trying to implement a system where I use a date picker to alter a date, but I am doing it on an alertDialog, and when I change the value with the date picker, it does not change in the alertDialog screen. the date remains the same unless I close and open again the alert dialog. How can I change the state in this case? Down the code:
updateInfo(BuildContext context) {
placaController = TextEditingController(text: widget.infoApanha.placa);
motoristaController =
TextEditingController(text: widget.infoApanha.motorista);
fimApanha = widget.infoApanha.dtFimApanha;
saidaAviario = widget.infoApanha.dtSaidaAviario;
AlertDialog alert = AlertDialog(
title: Center(child: Text(widget.infoApanha.granja)),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
CustomTextFormField(
title: 'PLACA DO VEÍCULO',
validator: validator,
controller: placaController,
),
CustomTextFormField(
title: 'MOTORISTA',
validator: validator,
controller: motoristaController,
),
const FittedBox(
fit: BoxFit.scaleDown,
child: Padding(
padding: EdgeInsets.fromLTRB(10, 8, 10, 0),
child: Text('DATA E HORA FIM EMBARQUE'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.red),
color: Colors.white,
borderRadius: BorderRadius.circular(10)),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Row(
children: [
Text(DateFormat('dd/MM/yyyy HH:mm').format(fimApanha)),
IconButton(
onPressed: pickFimApanha,
icon: const Icon(
Icons.calendar_month,
color: Colors.red,
),
),
],
),
),
),
),
const FittedBox(
fit: BoxFit.scaleDown,
child: Padding(
padding: EdgeInsets.fromLTRB(10, 8, 10, 0),
child: Text('DATA E HORA SAIDA AVIÁRIO'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: Container(
padding: const EdgeInsets.all(8),
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(color: Colors.red),
color: Colors.white,
borderRadius: BorderRadius.circular(10)),
child: FittedBox(
fit: BoxFit.scaleDown,
child: Row(
children: [
Text(DateFormat('dd/MM/yyyy HH:mm').format(saidaAviario)),
IconButton(
onPressed: pickSaidaAviario,
icon: const Icon(
Icons.calendar_month,
color: Colors.red,
),
),
],
),
),
),
),
],
),
actions: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Expanded(
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () async {
await Provider.of<InfoApanhaList>(context, listen: false)
.atualizaInfoApanha(
InfoApanha(
id: widget.infoApanha.id,
granja: widget.infoApanha.granja,
placa: placaController.text,
motorista: motoristaController.text,
dtFimApanha: fimApanha,
dtSaidaAviario: saidaAviario,
),
);
Navigator.pop(context);
},
child: const Text('ENVIAR'),
),
),
),
const SizedBox(width: 20,),
Expanded(
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('CANCELAR'),
),
),
)
],
),
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
DatePicker function:
Future pickFimApanha() async {
DateTime? date = await pickDate();
TimeOfDay? time = await pickTime();
if (time == null) {
return;
} else {
setState(() {
fimApanha = DateTime(
date!.year,
date.month,
date.day,
time.hour,
time.minute,
);
});
}
}
Wrap your Dialog with StatefulBuilder
widget like this
AlertDialog alert = AlertDialog(
title: Center(child: Text(widget.infoApanha.granja)),
content: StatefulBuilder(
builder: (context, alertSetState) {
return Column(
childern:[
// your code
]
);
}
and then use alertSetState
instead of setState like this
alertSetState(() {
fimApanha = DateTime(
date!.year,
date.month,
date.day,
time.hour,
time.minute,
);
});