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
相关产品推荐
相关产品推荐

