DateTimePicker not setting the field value which is passed

Viewed 435

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.

1 Answers

Update: To show initialValue you need to provide string on initialValue

DateTimePicker(
  initialValue: initDate.toString(), ///this

Demo Widget

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  late DateTime initDate;

  @override
  void initState() {
    super.initState();
    initDate = DateTime.now();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: DateTimePicker(
        initialValue: initDate.toString(),
        type: DateTimePickerType.date,
        firstDate: DateTime.now().subtract(Duration(days: 4)),
        lastDate: DateTime.now().add(Duration(days: 5 * 365)),
        initialDate: initDate,
        dateLabelText: 'Start Date',
        onChanged: (val) {},
        validator: (value) {
          if (value!.isEmpty) {
            return 'Please fill this field';
          }
          return null;
        },
      ),
    );
  }
}
Related