I am using date_time_picker and trying to set the date in date picker input field but it is not setting that value which is being passed instead it always set my current date.
Here is my code snippet. This is the datetime picker input field.
DateTimePicker(
type: DateTimePickerType.date,
dateMask: 'yyyy/MM/dd',
controller: startDateTxt,
firstDate: DateTime.now(),
lastDate: DateTime.now().add(Duration(days: 5 * 365)),
initialDate: initStartDate,
icon: Icon(Icons.event),
dateLabelText: 'Start Date',
onChanged: (val) => setState(() => startDateTxt.text = val),
validator: (value) {
if (value!.isEmpty) {
return 'Please fill this field';
}
return null;
},
onSaved: (val) => setState(() => startDateTxt.text = val ?? ''),
decoration: InputDecoration(
labelText: "Start Date",
labelStyle: TextStyle(color: Colors.grey[700]),
filled: true,
fillColor: Colors.white,
contentPadding:
EdgeInsets.symmetric(horizontal: 12, vertical: 15),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.2))),
focusedBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.5))),
enabledBorder: OutlineInputBorder(
borderSide: BorderSide(
color: Theme.of(context).focusColor.withOpacity(0.2))),
),
),
An I am setting the date like this.
startDateTxt.text = DateFormat('yyyy/MM/dd')
.format(DateFormat('MM/dd/yyyy').parse(cat.startDate));
endDateTxt.text = DateFormat('yyyy/MM/dd')
.format(DateFormat('MM/dd/yyyy').parse(cat.endDate));
setState(() {
eventCatSelected = true;
initStartDate = DateFormat('yyyy/MM/dd').parse(startDateTxt.text);
initEndDate = DateFormat('yyyy/MM/dd').parse(endDateTxt.text);
});
When I run this code it always show my current date in input field, not that one which I am setting using above code.