skip to Main Content

I want to limit the dates iOS date picker shows, so it should only display three dates: yesterday, today, and tomorrow. I tried setting a minimum and maximum date range, but it still shows past and future dates in the date picker UI.

enter image description here

3

Answers


  1. For CupertinoDatePicker, you need to pass

    initialDateTime: DateTime.now(),
    minimumDate: DateTime.now().subtract(const Duration(days: 1)),
    maximumDate:DateTime.now().add(const Duration(days: 1)),
    

    For showDatePicker, you need to pass

    initialDateTime: DateTime.now(),
    firstDate: DateTime.now().subtract(const Duration(days: 1)),
    lastDate: DateTime.now().add(const Duration(days: 1)),
    

    Here is showDatePicker example:

    await showDatePicker(
       context: context,
       initialDate: DateTime.now(),
       firstDate:DateTime.now().subtract(const Duration(days: 1)),
       lastDate: DateTime.now().add(const Duration(days: 1)),
       initialEntryMode: DatePickerEntryMode.calendarOnly,
       builder: (context, child) {
             return Theme(
                   data: ThemeData(
                      primaryColor: Colors.blue,
                      colorScheme: ColorScheme.fromSwatch()
                                        .copyWith(secondary: Colors.blue)),
                       child: child!,
              );
       }
    );
    
    Login or Signup to reply.
  2. Try below code, refer CupertinoDatePicker, maximumDate and minimumDate, you need to add intialDate, maximum and minimum date for your requirement.

    CupertinoButton(
                onPressed: () => _showDialog(
                  CupertinoDatePicker(
                    minimumDate: DateTime.now().subtract(const Duration(days: 1)),
                    maximumDate: DateTime.now().add(const Duration(days: 1)),
                    initialDateTime: DateTime.now(),
                    use24hFormat: false,
                    onDateTimeChanged: (DateTime newDateTime) {
                      setState(() => dateTime = newDateTime);
                    },
                  ),
                ),
                child: Text(
                  '${dateTime.month}-${dateTime.day}-${dateTime.year} ${dateTime.hour}:${dateTime.minute}',
                  style: const TextStyle(
                    fontSize: 22.0,
                  ),
                ),
              ),
    

    Result-> image

    Login or Signup to reply.
  3. In CupertinoDatePicker there are two parameters available you just need to pass date in right way

    minimumDate : DateTime.now().subtract(const Duration(days:1)),

    so it will calculate the day before todays date

    maximumDate : DateTime.now().add(const Duration(days:1)),

    it will add the day in current date

    full code is like below

       showCupertinoModalPopup(
           context: context,
           builder: (_) => Container(
                 height: 190,
                 color: const Color.fromARGB(
                     255, 255, 255, 255),
                 child: Column(
                   children: [
                     SizedBox(
                       height: 180,
                       child:
                           CupertinoDatePicker(
                               minimumDate: DateTime
                                       .now()
                                   .subtract(
                                       const Duration(
                                           days:
                                               1)),
                               maximumDate: DateTime
                                       .now()
                                   .add(const Duration(
                                       days: 1)),
                               initialDateTime:
                                   DateTime
                                       .now(),
                               onDateTimeChanged:
                                   (val) {
                                 setState(() {});
                               }),
                     ),
                   ],
                 ),
               ));
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search