day_cell.dart 2.3 KB

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