Home > Software design >  How to change CupertinoDatePicker dateFormat?
How to change CupertinoDatePicker dateFormat?

Time:12-26

enter image description here

As you can see the dateFormat of CupertinoDatePicker is year month day.

But I want show year and month only.

How can I do it?

Here is my code:

SizedBox(
  width: 500,
  height: 250,
  child: CupertinoDatePicker(
    mode: CupertinoDatePickerMode.date,
    backgroundColor: Colors.black54,
    dateOrder: DatePickerDateOrder.ymd,
    onDateTimeChanged: (date) {
      print(date);
    },
  ),
),

CodePudding user response:

You cannot hide it directly as there is no such method for hiding it. But what you can do is go to the '/Users/your_user_name/developer/flutter/packages/flutter/lib/src/cupertino/date_picker.dart' and copy the complete code from that file. Now go to your project directory and create a new dart file with anyName.dart and paste the whole code there. And search for the below Text widget in the file.

          Text(
              localizations.datePickerDayOfMonth(day),
              style:
                  _themeTextStyle(context, isValid: day <= daysInCurrentMonth),
            ),

Here replace the localizations.datePickerDayOfMonth(day) with empty string ' ' like that. Then where you need to use the customed Picker call it like:

import 'package:local_app/ui/widgets/anyName.dart' as customizedDatePicker;

and use it

  customizedDatePicker.CupertinoDatePicker(
   
    initialDateTime: DateTime.now(),
    mode: customizedDatePicker.CupertinoDatePickerMode.date,
  ),

CodePudding user response:

Please try to use enter image description here

copy the code from URL and save it anywhere in the lib. use code

CustomCupertinoDatePicker(
            mode: CustomCupertinoDatePickerMode.yearAndMonth,
            dateOrder: DatePickerDateOrder.dmy,
            backgroundColor: Colors.black54,
            onDateTimeChanged: (date) {
              print(date);
            },
          )
  • Related