month_calendar_item.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import 'package:calendar_view/calendar_view/calendar_util.dart';
  2. import 'package:calendar_view/calendar_view/chinese_calendar_utils.dart';
  3. import 'package:calendar_view/calendar_view/month_calendar/schedule_list.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:intl/intl.dart';
  6. /// 月历的每一格日期
  7. class MonthDayItem extends StatefulWidget {
  8. const MonthDayItem(
  9. {super.key,
  10. required this.dayData,
  11. required this.onSelect,
  12. required this.maxScheduleLines});
  13. final DayStructure dayData;
  14. final ValueChanged<int> onSelect;
  15. final int maxScheduleLines;
  16. /// 最大能容纳的日程条数
  17. @override
  18. State<MonthDayItem> createState() => _MyWidgetState();
  19. }
  20. class _MyWidgetState extends State<MonthDayItem> {
  21. late DayStructure _dayData;
  22. late BoxDecoration _dateContainerDecoration;
  23. BoxDecoration _gridContainerDecoration = const BoxDecoration(
  24. color: Colors.white,
  25. );
  26. late TextStyle _dayTextStyle;
  27. late TextStyle _lunarDayTextStyle;
  28. /// TODO[Gavin]: i18n
  29. get displayStr => _dayData.isToday ? '今' : _dayData.date.day.toString();
  30. @override
  31. void initState() {
  32. super.initState();
  33. _dayData = widget.dayData;
  34. _initStyle();
  35. }
  36. @override
  37. void didUpdateWidget(MonthDayItem oldWidget) {
  38. super.didUpdateWidget(oldWidget);
  39. _dayData = widget.dayData;
  40. _initStyle();
  41. }
  42. /// 初始化样式
  43. /// 存在三种背景样式,选中,未选中,“今日”未选中
  44. /// 存在四种文字样式,选中,未选中,非当月,“今日”未选中
  45. void _initStyle() {
  46. if (_dayData.isSelected) {
  47. _dayTextStyle = TextStyle(
  48. height: _dayData.isToday ? null : 1,
  49. fontSize: 14,
  50. color: Colors.white,
  51. );
  52. _dateContainerDecoration = BoxDecoration(
  53. color: Colors.blue,
  54. borderRadius: BorderRadius.circular(15),
  55. );
  56. _gridContainerDecoration = const BoxDecoration(
  57. color: Color.fromARGB(255, 226, 241, 254),
  58. );
  59. _lunarDayTextStyle = _dayTextStyle.copyWith(
  60. fontSize: 11,
  61. color: Colors.blue,
  62. );
  63. } else {
  64. if (_dayData.isToday) {
  65. _dayTextStyle = const TextStyle(
  66. height: null,
  67. fontSize: 14,
  68. color: Colors.blue,
  69. );
  70. _dateContainerDecoration = BoxDecoration(
  71. color: const Color.fromARGB(255, 224, 241, 255),
  72. borderRadius: BorderRadius.circular(15),
  73. );
  74. _gridContainerDecoration = const BoxDecoration(
  75. color: Color.fromARGB(255, 248, 248, 248),
  76. );
  77. _lunarDayTextStyle = _dayTextStyle.copyWith(
  78. color: Colors.black87,
  79. fontSize: 11,
  80. );
  81. } else {
  82. _dayTextStyle = TextStyle(
  83. height: _dayData.isToday ? null : 1,
  84. fontSize: 14,
  85. color: _dayData.isCurrentMonth ? Colors.black87 : Colors.black26,
  86. );
  87. _dateContainerDecoration = const BoxDecoration(
  88. color: Colors.transparent,
  89. );
  90. _gridContainerDecoration = const BoxDecoration(
  91. color: Colors.white,
  92. );
  93. _lunarDayTextStyle = _dayTextStyle.copyWith(
  94. fontSize: 11,
  95. );
  96. }
  97. }
  98. }
  99. @override
  100. Widget build(BuildContext context) {
  101. return MouseRegion(
  102. cursor: SystemMouseCursors.click,
  103. child: GestureDetector(
  104. onTap: () {
  105. widget.onSelect.call(_dayData.index);
  106. },
  107. child: Container(
  108. decoration: _gridContainerDecoration,
  109. child: Column(
  110. children: [
  111. Row(
  112. mainAxisAlignment: MainAxisAlignment.center,
  113. children: [
  114. Container(
  115. decoration: _dateContainerDecoration,
  116. margin: const EdgeInsets.symmetric(vertical: 3),
  117. height: 24,
  118. width: 24,
  119. child: Center(
  120. child: Text(displayStr, style: _dayTextStyle),
  121. ),
  122. ),
  123. Container(
  124. // decoration: _dateContainerDecoration,
  125. margin: const EdgeInsets.all(3),
  126. height: 24,
  127. child: Center(
  128. child: Text(_getLunarDay(), style: _lunarDayTextStyle),
  129. ),
  130. ),
  131. ],
  132. ),
  133. _buildScheduleContainer()
  134. ],
  135. ),
  136. ),
  137. ),
  138. );
  139. }
  140. /// 构建日程容器
  141. Widget _buildScheduleContainer() {
  142. return Expanded(
  143. // ignore: sized_box_for_whitespace
  144. child: Container(
  145. width: double.infinity,
  146. child: ScheduleList(maxLines: widget.maxScheduleLines),
  147. ),
  148. );
  149. }
  150. /// 获取节气和农历日
  151. String _getLunarDay() {
  152. DateTime date = _dayData.date;
  153. CalendarInfo lunarDate = CalendarUtils.getInfo(date);
  154. return lunarDate.term ?? lunarDate.lunarDayName ?? '';
  155. }
  156. }