Flutter How to pick the previous date that are disabled?

1.3k Views Asked by At

Date picker screenshot

hello I had a code here that can pick date and time but my problem is I cannot pick in the previous dates. who had an idea on how to open all the dates. like in my screenshot the 1 to 4 dates are disabled i want it to be open.

This is my Time format

  final DateFormat dateFormat8 = DateFormat('MM-dd-yyyy HH:mm');
      TimeOfDay selecttime8 = new TimeOfDay.now();
      Future<TimeOfDay> _selectedTime8(BuildContext context) {
        return showTimePicker(context: context, initialTime: selecttime8);
      }

This is my Date format.

DateTime selectdate8;
  Future<DateTime> _selectDate8(BuildContext context) => showDatePicker(
      context: context,
      initialDate: DateTime.now().add(Duration(seconds: 1)),
      firstDate: DateTime.now(),
      lastDate: DateTime(3000));

This is the code in my scaffold when the user click the inputfield.

Padding(
                    padding:
                        EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
                    child: InkWell(
                      onTap: () async {
                        final selectdate8 = await _selectDate8(context);
                        if (selectdate8 == null) return;

                        final selecttime8 = await _selectedTime8(context);
                        if (selecttime8 == null) return;

                        setState(() {
                          this.selectdate8 = DateTime(
                            selectdate8.year,
                            selectdate8.month,
                            selectdate8.day,
                            selecttime8.hour,
                            selecttime8.minute,
                          );
                        });
                      },
1

There are 1 best solutions below

0
On BEST ANSWER

The problem is here, as mentioned by Randal Schwartz:

firstDate: DateTime.now()

Now, what you can do is select a year.

firstDate: DateTime.utc(1965, 1, 1)

DateTime.utc(yyyy, mm, dd)