我只需要在日期选择器中显示年份和月份,那么如何从日期选择器中隐藏日期。
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,
),
希望它能帮助别人并节省我一直在为我的问题寻找简单快捷的解决方案的时间。
关于dart - 我怎样才能从库比蒂诺日期选择器中隐藏日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55475426/