i need to pic only year and month from the date picker, so how can i hide day from date picker.
CupertinoDatePicker(
initialDateTime: DateTime.now(),
onDateTimeChanged: (DateTime newdate) {
print(newdate);
widget.card.expDateTime = newdate.toString();
dateCnt.text = newdate.toString().split(" ")[0];
},
minimumYear: DateTime.now().year,
minimumDate: DateTime.now(),
mode: CupertinoDatePickerMode.date,
)
I had this problem, tried the package flutter_cupertino_date_picker but it looks like it don't have the hability to format only month and year excluding day, so you need to program on it to extend the capabilities. To me seemed more logical to change the build in CupertinoDatePicker that comes with Flutter, what I did was copy all the content of '/Users/your_user_name/developer/flutter/packages/flutter/lib/src/cupertino/date_picker.dart' in another file in my local envirronment, I called cupertino_picker_extended.dart
, then (because I wanted a quick way) on line 1182 I changed: Text(localizations.datePickerDayOfMonth(day),...
for Text('',...
Then where you need to use the customed Picker call it like:
import 'package:local_app/ui/widgets/cupertino_picker_extended.dart' as CupertinoExtended;
and use it:
CupertinoExtended.CupertinoDatePicker(
onDateTimeChanged: (DateTime value) {
setDate('${value.month}/${value.year}', setDateFunction,
section, arrayPos);
},
initialDateTime: DateTime.now(),
mode: CupertinoExtended.CupertinoDatePickerMode.date,
),
This is the result:
Hope it help someone and save the time I been looking for a easy and quick solution for my problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With