month_calendar.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import 'dart:html';
  2. import 'package:calendar_view/calendar_controller/controller.dart';
  3. import 'package:calendar_view/utils/calendar_util.dart';
  4. import 'package:calendar_view/calendar_page/month_calendar/month_calendar_item.dart';
  5. import 'package:flutter/foundation.dart';
  6. import 'package:flutter/material.dart';
  7. import 'package:get/get.dart';
  8. class MonthCalendar extends StatefulWidget {
  9. const MonthCalendar({super.key});
  10. @override
  11. State<MonthCalendar> createState() => _MonthCalendarState();
  12. }
  13. class _MonthCalendarState extends State<MonthCalendar> {
  14. List<MonthViewDayStructure> monthViewDaysList = []; // 当前控制器的日期列表
  15. CalendarController calendarController = Get.find<CalendarController>();
  16. int _maxScheduleLines = 3; // 最大的日程行数【需要动态计算】
  17. double _viewHeight = 0; // 维护一个视口高度,如果视口高度不变,那么日程的高度不需要计算
  18. int _calendarLines = 0; // 维护一个日历行数,如果日历行数不变,那么日程的高度不需要计算
  19. @override
  20. void initState() {
  21. super.initState();
  22. /// 监控resize事件,动态计算最大日程行数
  23. WidgetsBinding.instance.addPostFrameCallback((_) {
  24. _handleMaxScheduleLines();
  25. });
  26. /// web端监听resize事件
  27. if (kIsWeb) {
  28. window.onResize.listen((event) {
  29. WidgetsBinding.instance.addPostFrameCallback((_) {
  30. _handleMaxScheduleLines();
  31. });
  32. });
  33. }
  34. monthViewDaysList = calendarController.monthViewDaysList;
  35. calendarController.onDaysListChange.addListener(_onDaysListChange);
  36. }
  37. @override
  38. void dispose() {
  39. super.dispose();
  40. calendarController.onDaysListChange.removeListener(_onDaysListChange);
  41. }
  42. void _onDaysListChange(_, e) {
  43. setState(() {
  44. monthViewDaysList = calendarController.monthViewDaysList;
  45. });
  46. _handleMaxScheduleLines();
  47. }
  48. @override
  49. Widget build(BuildContext context) {
  50. return Column(
  51. children: <Widget>[
  52. _buildMonthCalendarWeekTitle(),
  53. Expanded(child: _buildMonthCalendarDaysBody()),
  54. ],
  55. );
  56. }
  57. /// 构建「日、一、二、三、四、五、六」的标题
  58. Widget _buildMonthCalendarWeekTitle() {
  59. return SizedBox(
  60. height: 30,
  61. child: Row(
  62. mainAxisAlignment: MainAxisAlignment.spaceAround,
  63. children: <Widget>[
  64. /// TODO[Gavin]: i18n
  65. _buildEachTitle('周日'),
  66. _buildEachTitle('周一'),
  67. _buildEachTitle('周二'),
  68. _buildEachTitle('周三'),
  69. _buildEachTitle('周四'),
  70. _buildEachTitle('周五'),
  71. _buildEachTitle('周六'),
  72. ],
  73. ),
  74. );
  75. }
  76. Widget _buildEachTitle(String title) {
  77. const weekTextStyle = TextStyle(fontSize: 12, color: Colors.black);
  78. return SizedBox(
  79. width: 25,
  80. child: Text(
  81. title,
  82. style: weekTextStyle,
  83. textAlign: TextAlign.center,
  84. ),
  85. );
  86. }
  87. /// 构建日历主体
  88. Widget _buildMonthCalendarDaysBody() {
  89. // 将35天分成5个周
  90. final weeksInMonth = <List<MonthViewDayStructure>>[];
  91. for (var i = 0; i < monthViewDaysList.length; i += 7) {
  92. weeksInMonth.add(monthViewDaysList.sublist(i, i + 7));
  93. }
  94. return SizedBox(
  95. child: Column(
  96. mainAxisSize: MainAxisSize.max,
  97. mainAxisAlignment: MainAxisAlignment.spaceAround,
  98. children: weeksInMonth.map(_buildMonthCalendarDayRow).toList(),
  99. ),
  100. );
  101. }
  102. /// 构建每一行(周)
  103. Widget _buildMonthCalendarDayRow(List<MonthViewDayStructure> sevenDays) {
  104. return Expanded(
  105. child: Row(
  106. mainAxisAlignment: MainAxisAlignment.spaceAround,
  107. children: sevenDays.map(_buildEachDay).toList(),
  108. ),
  109. );
  110. }
  111. /// 构建每一天
  112. Widget _buildEachDay(MonthViewDayStructure day) {
  113. return Expanded(
  114. child: Container(
  115. decoration: const BoxDecoration(
  116. border: Border(
  117. right: BorderSide(
  118. color: Colors.black12,
  119. width: 1,
  120. ),
  121. top: BorderSide(
  122. color: Colors.black12,
  123. width: 1,
  124. ),
  125. ),
  126. ),
  127. child: MonthDayItem(
  128. dayData: day,
  129. maxScheduleLines: _maxScheduleLines,
  130. ),
  131. ),
  132. );
  133. }
  134. /// 动态计算日历格内的最大日程行数
  135. void _handleMaxScheduleLines() {
  136. final size = MediaQuery.of(context).size;
  137. if (size.height == _viewHeight &&
  138. calendarController.calendarLines == _calendarLines) return;
  139. _viewHeight = size.height;
  140. _calendarLines = calendarController.calendarLines;
  141. final gridRows = calendarController.calendarLines;
  142. final gridHeight = size.height - 100; // 100 是日历头部的高度
  143. final gridItemHeight = gridHeight / gridRows;
  144. final scheduleAreaHeight = gridItemHeight - 30; // 30 是日期的高度
  145. final maxLines = (scheduleAreaHeight / 20).floor();
  146. setState(() {
  147. _maxScheduleLines = maxLines;
  148. });
  149. }
  150. }