I have used the below code to save the date time. It selects both date and time and displays the selected date and time in the textfield. But how do I save it in a variable or how do I save it in my database. I have 'note' table in database. I want to save the selected date and time in 'actn_on' column in 'note' table.
Text('Action on (${format.pattern})'),
DateTimeField(
format: format,
onShowPicker: (context, currentValue) async {
final date = await showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
lastDate: DateTime(2100));
if (date != null) {
final time = await showTimePicker(
context: context,
initialTime:
TimeOfDay.fromDateTime(currentValue ?? DateTime.now()),
);
return DateTimeField.combine(date, time);
} else {
return currentValue;
}
},
),
CodePudding user response:
At First, Declare a Controller,
final TextEditingController _timeController = TextEditingController();
then pass it to the
DateTimeField
like this
DateTimeField(
controller: _timeController,
....
)
access it like _timeController.text
Or else DateTimeField
also offer onSaved
, to use this, first declare a variable var _time
and then pass it like this
DateTimeField(
onSaved: (val) => setState(() => _time = val),
....
)
CodePudding user response:
I belive your answer is in this field:
return currentValue;
Just make a variable outside and assign currentValue
to it.Like:
class MyWidget extends StatelessWidget{
String dt = "";
Widget build(BuildContext c){
// your code
.....
else {
dt = currentValue;
return currentValue;
}
}
}