day_table_view.dart 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import 'package:flutter/material.dart';
  2. import 'day_cell.dart';
  3. /// 一周的数据为一行,展示两周共两行
  4. class DayTableView extends StatelessWidget {
  5. const DayTableView({
  6. super.key,
  7. required this.weekdays,
  8. required this.onSelect,
  9. required this.selectedDate,
  10. required this.currentDate,
  11. });
  12. final List<DateTime> weekdays;
  13. final Function(DateTime)? onSelect;
  14. final DateTime selectedDate;
  15. final DateTime currentDate;
  16. @override
  17. Widget build(BuildContext context) {
  18. return Column(
  19. children: [
  20. // 第一行
  21. Row(
  22. mainAxisAlignment: MainAxisAlignment.spaceAround,
  23. children: weekdays.sublist(0, 7).map(
  24. (date) {
  25. return GestureDetector(
  26. onTap: _isPastDate(date) ? null : () => onSelect?.call(date),
  27. child: SizedBox(
  28. width: 40,
  29. height: 40,
  30. child: DayCell(
  31. display: date,
  32. selected: selectedDate,
  33. current: currentDate,
  34. ),
  35. ),
  36. );
  37. },
  38. ).toList(),
  39. ),
  40. const SizedBox(height: 10),
  41. // 第二行
  42. Row(
  43. mainAxisAlignment: MainAxisAlignment.spaceAround,
  44. children: weekdays.sublist(7, 14).map(
  45. (date) {
  46. return GestureDetector(
  47. onTap: _isPastDate(date) ? null : () => onSelect?.call(date),
  48. child: SizedBox(
  49. width: 40,
  50. height: 40,
  51. child: DayCell(
  52. display: date,
  53. selected: selectedDate,
  54. current: currentDate,
  55. ),
  56. ),
  57. );
  58. },
  59. ).toList(),
  60. ),
  61. ],
  62. );
  63. }
  64. // 检查日期是否小于今天
  65. bool _isPastDate(DateTime date) {
  66. return date.isBefore(DateTime.now().subtract(const Duration(days: 1))); // 小于今天
  67. }
  68. }