You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改CupertinoDatePicker使其仅显示年份和月份?

如何让CupertinoDatePicker仅显示年份和月份?

CupertinoDatePicker 当前显示年日月

当前CupertinoDatePicker的日期格式为年、月、日,但我希望仅显示年份和月份,请问该如何实现?

我的现有代码:

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

直接修改CupertinoDatePicker的mode参数为CupertinoDatePickerMode.monthYear即可实现仅显示年份和月份的效果,同时可以把dateOrder调整为DatePickerDateOrder.ym更贴合需求:

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

这个模式下,选择器只会展示年份和月份的滚轮,完全符合需求。


内容的提问来源于stack exchange,提问作者无夜之星辰

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 10:10:15