Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i hide day from cupertino date picker

Tags:

flutter

dart

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,
)
like image 273
Deepak Gehlot Avatar asked Apr 02 '19 12:04

Deepak Gehlot


1 Answers

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: screenshot

Hope it help someone and save the time I been looking for a easy and quick solution for my problem.

like image 86
Daniel Avatar answered Sep 21 '22 10:09

Daniel