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

Flutter日期选择器中如何修改当前日期的主题样式?

解决Flutter日期选择器当前日期样式无法修改的问题

默认通过ColorScheme配置主题时,不会覆盖日期选择器中**当前日期(today)**的专属样式,需要在ThemeData中单独配置datePickerTheme来定制。

修改后的代码示例:

final DateTime? picked = await showDatePicker(
  context: context,
  initialDate: null,
  firstDate: DateTime(1900, 8),
  lastDate: DateTime.now(),
  builder: (context, child) {
    return Theme(
      data: Theme.of(context).copyWith(
        colorScheme: ColorScheme.light(
          primary: Colors.white,
          onPrimary: primary,
          onSurface: Colors.black,
        ),
        textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(foregroundColor: primary),
        ),
        // 新增当前日期样式配置
        datePickerTheme: DatePickerThemeData(
          todayBackgroundColor: MaterialStateColor.resolveWith((states) => Colors.blue), // 当前日期背景色
          todayTextColor: Colors.white, // 当前日期文字颜色
          // 可选:配置选中日期的样式
          selectedDayBackgroundColor: MaterialStateColor.resolveWith((states) => Colors.white),
          selectedDayTextColor: primary,
        ),
      ),
      child: child!,
    );
  },
);

关键配置项说明:

  • todayBackgroundColor:控制当前日期的背景颜色,需用MaterialStateColor适配不同状态
  • todayTextColor:控制当前日期的文字颜色
  • selectedDayBackgroundColor:已选中日期的背景色
  • selectedDayTextColor:已选中日期的文字颜色

内容的提问来源于stack exchange,提问作者Aliasgar Kuwakhedawala

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 03:12:03