day_cell.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'package:cs_resources/theme/app_colors_theme.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:shared/utils/date_time_utils.dart';
  4. class DayCell extends StatelessWidget {
  5. const DayCell({
  6. super.key,
  7. required this.display,
  8. required this.selected,
  9. required this.current,
  10. });
  11. final DateTime display;
  12. final DateTime selected;
  13. final DateTime current;
  14. //显示的日期文本
  15. String get dayText {
  16. return DateTimeUtils.formatDate(display, format: 'dd');
  17. }
  18. @override
  19. Widget build(BuildContext context) {
  20. return AnimatedContainer(
  21. duration: const Duration(milliseconds: 300),
  22. width: 40,
  23. height: 40,
  24. decoration: BoxDecoration(
  25. color: _isPastDate(display)
  26. ? context.appColors.disEnableGray // 小于今天的日期背景颜色,禁用
  27. : _isSelected(display)
  28. ? _isToday(display)
  29. ? context.appColors.btnBgDefault // 今天的日期背景颜色
  30. : context.appColors.btnBgDefault // 选中的日期背景颜色
  31. : context.appColors.grayBgE9, // 默认的背景颜色
  32. shape: BoxShape.rectangle,
  33. borderRadius: BorderRadius.circular(2.5), // 圆角
  34. ),
  35. alignment: Alignment.center,
  36. child: Text(
  37. dayText,
  38. style: TextStyle(
  39. fontSize: 17,
  40. fontWeight: FontWeight.w500,
  41. color: _dayTextColor(context, display),
  42. ),
  43. ),
  44. );
  45. }
  46. Color _dayTextColor(BuildContext context, DateTime date) {
  47. if (_isSelected(date)) {
  48. return Colors.white;
  49. }
  50. if (_isToday(date)) {
  51. return context.appColors.textBlack;
  52. }
  53. if (_isWeekend(date)) {
  54. return context.appColors.textBlack;
  55. }
  56. return context.appColors.textBlack;
  57. }
  58. //是否是周末
  59. bool _isWeekend(DateTime date) {
  60. return date.weekday == DateTime.saturday || date.weekday == DateTime.sunday;
  61. }
  62. //是否是选中的
  63. bool _isSelected(DateTime date) {
  64. return DateTimeUtils.formatDate(date, format: 'yyyyMMdd') == DateTimeUtils.formatDate(selected, format: 'yyyyMMdd');
  65. }
  66. //是否是今天
  67. bool _isToday(DateTime date) {
  68. return DateTimeUtils.formatDate(date, format: 'yyyyMMdd') == DateTimeUtils.formatDate(current, format: 'yyyyMMdd');
  69. }
  70. // 检查日期是否小于今天
  71. bool _isPastDate(DateTime date) {
  72. return date.isBefore(DateTime.now().subtract(const Duration(days: 1))); // 小于今天
  73. }
  74. }