month_calendar_item.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import 'package:calendar_view/calendar_controller/controller.dart';
  2. import 'package:calendar_view/popup_layer/popup_layer_controller.dart';
  3. import 'package:calendar_view/utils/calendar_util.dart';
  4. import 'package:calendar_view/utils/chinese_calendar_utils.dart';
  5. import 'package:calendar_view/calendar_page/month_calendar/schedule_list.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:get/get.dart';
  8. /// 月历的每一格日期
  9. class MonthDayItem extends StatefulWidget {
  10. const MonthDayItem(
  11. {super.key,
  12. required this.dayData,
  13. // required this.onSelect,
  14. required this.maxScheduleLines});
  15. final MonthViewDayStructure dayData;
  16. // final ValueChanged<int> onSelect;
  17. final int maxScheduleLines;
  18. /// 最大能容纳的日程条数
  19. @override
  20. State<MonthDayItem> createState() => _MyWidgetState();
  21. }
  22. class _MyWidgetState extends State<MonthDayItem> {
  23. late MonthViewDayStructure _dayData;
  24. late BoxDecoration _dateContainerDecoration;
  25. CalendarController calendarController = Get.find<CalendarController>();
  26. PopupLayerController popupLayerController = Get.find<PopupLayerController>();
  27. BoxDecoration _gridContainerDecoration = const BoxDecoration(
  28. color: Colors.white,
  29. );
  30. late TextStyle _dayTextStyle;
  31. late TextStyle _lunarDayTextStyle;
  32. final GlobalKey _monthGridKey = GlobalKey();
  33. /// TODO[Gavin]: i18n
  34. get displayStr => _dayData.isToday ? '今' : _dayData.date.day.toString();
  35. @override
  36. void initState() {
  37. super.initState();
  38. _dayData = widget.dayData;
  39. _initStyle();
  40. }
  41. @override
  42. void didUpdateWidget(MonthDayItem oldWidget) {
  43. super.didUpdateWidget(oldWidget);
  44. _dayData = widget.dayData;
  45. _initStyle();
  46. }
  47. /// 初始化样式
  48. /// 存在三种背景样式,选中,未选中,“今日”未选中
  49. /// 存在四种文字样式,选中,未选中,非当月,“今日”未选中
  50. void _initStyle() {
  51. if (_dayData.isSelected) {
  52. _dayTextStyle = TextStyle(
  53. height: _dayData.isToday ? null : 1,
  54. fontSize: 14,
  55. color: Colors.white,
  56. );
  57. _dateContainerDecoration = BoxDecoration(
  58. color: Colors.blue,
  59. borderRadius: BorderRadius.circular(15),
  60. );
  61. _gridContainerDecoration = const BoxDecoration(
  62. color: Color.fromARGB(255, 226, 241, 254),
  63. );
  64. _lunarDayTextStyle = _dayTextStyle.copyWith(
  65. fontSize: 11,
  66. color: Colors.blue,
  67. );
  68. } else {
  69. if (_dayData.isToday) {
  70. _dayTextStyle = const TextStyle(
  71. height: null,
  72. fontSize: 14,
  73. color: Colors.blue,
  74. );
  75. _dateContainerDecoration = BoxDecoration(
  76. color: const Color.fromARGB(255, 224, 241, 255),
  77. borderRadius: BorderRadius.circular(15),
  78. );
  79. _gridContainerDecoration = const BoxDecoration(
  80. color: Color.fromARGB(255, 248, 248, 248),
  81. );
  82. _lunarDayTextStyle = _dayTextStyle.copyWith(
  83. color: Colors.black87,
  84. fontSize: 11,
  85. );
  86. } else {
  87. _dayTextStyle = TextStyle(
  88. height: _dayData.isToday ? null : 1,
  89. fontSize: 14,
  90. color: _dayData.isCurrentMonth ? Colors.black87 : Colors.black26,
  91. );
  92. _dateContainerDecoration = const BoxDecoration(
  93. color: Colors.transparent,
  94. );
  95. _gridContainerDecoration = const BoxDecoration(
  96. color: Colors.white,
  97. );
  98. _lunarDayTextStyle = _dayTextStyle.copyWith(
  99. fontSize: 11,
  100. );
  101. }
  102. }
  103. }
  104. @override
  105. Widget build(BuildContext context) {
  106. return Container(
  107. key: _monthGridKey,
  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. Widget _buildScheduleContainer() {
  140. return Expanded(
  141. // ignore: sized_box_for_whitespace
  142. child: Container(
  143. width: double.infinity,
  144. child: ScheduleList(
  145. maxLines: widget.maxScheduleLines,
  146. scheduleDataList: calendarController
  147. .scheduleListFilter(widget.dayData.scheduleList),
  148. onTapShowMore: () {
  149. popupLayerController.handlePopupMoreSchedule(
  150. _monthGridKey,
  151. widget.dayData,
  152. );
  153. },
  154. ),
  155. ),
  156. );
  157. }
  158. /// 获取节气和农历日
  159. String _getLunarDay() {
  160. DateTime date = _dayData.date;
  161. CalendarInfo lunarDate = CalendarUtils.getInfo(date);
  162. return lunarDate.term ?? lunarDate.lunarDayName ?? '';
  163. }
  164. }