dart - 我怎样才能从库比蒂诺日期选择器中隐藏日期

标签 dart flutter

我只需要在日期选择器中显示年份和月份,那么如何从日期选择器中隐藏日期。

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,
)

最佳答案

我遇到了这个问题,尝试了包 flutter_cupertino_date_picker但它看起来不能只格式化月份和年份(不包括日期),因此您需要对其进行编程以扩展功能。对我来说,更改 Flutter 附带的 CupertinoDatePicker 中的构建似乎更合乎逻辑,我所做的是复制 '/Users/your_user_name/developer/flutter/packages/flutter/lib/src/cupertino/date_picker.dart' 中的所有内容我本地环境中的另一个文件,我调用了 cupertino_picker_extended.dart,然后(因为我想要一个快速的方法)在第 1182 行我更改了:Text(localizations.datePickerDayOfMonth(day),... 用于 Text('',...

然后在需要使用自定义 Picker 的地方调用它:

import 'package:local_app/ui/widgets/cupertino_picker_extended.dart' as CupertinoExtended;

并使用它:

  CupertinoExtended.CupertinoDatePicker(
    onDateTimeChanged: (DateTime value) {
      setDate('${value.month}/${value.year}', setDateFunction,
          section, arrayPos);
    },
    initialDateTime: DateTime.now(),
    mode: CupertinoExtended.CupertinoDatePickerMode.date,
  ),

这是结果: screenshot

希望它能帮助别人并节省我一直在为我的问题寻找简单快捷的解决方案的时间。

关于dart - 我怎样才能从库比蒂诺日期选择器中隐藏日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55475426/

相关文章:

flutter - 如何根据选择的第一项 flutter 填充第二个下拉菜单

dart - IndexedDb:database.close() 挂起

dart - 兼容 Flutter 和 javascript 的 AES 加解密

flutter - 如何在上下文中的Flutter中结合DropdownMenu和AppLocalization?

flutter - 我可以向下转换一个类,使它丢失原始的构造函数吗?

dart - flutter 如何弹出动态页面?

android - 无法获取 'play-services-location/maven-metadata.xml' 。从服务器 : Bad Gateway 收到状态码 502

flutter - _断言错误('package:firebase_auth/src/firebase_auth.dart' : Failed assertion: line 95 pos 12: 'email != null' : is not true.)

Flutter:使用带有卡住和 json_serializable 的 Map

dart - 升级 Dart 后 Angular Dart 构建失败