month_calendar_item.dart 4.5 KB

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