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.
3
For CupertinoDatePicker, you need to pass
CupertinoDatePicker
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
showDatePicker
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!, ); } );
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->
In CupertinoDatePicker there are two parameters available you just need to pass date in right way
minimumDate : DateTime.now().subtract(const Duration(days:1)),
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)),
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(() {}); }), ), ], ), ));
Click here to cancel reply.
3
Answers
For
CupertinoDatePicker
, you need to passFor
showDatePicker
, you need to passHere is
showDatePicker
example:Try below code, refer CupertinoDatePicker, maximumDate and minimumDate, you need to add intialDate, maximum and minimum date for your requirement.
Result->
In
CupertinoDatePicker
there are two parameters available you just need to pass date in right wayminimumDate
: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