Selaa lähdekoodia

更新mock数据接口

gavin.chen 2 vuotta sitten
vanhempi
commit
3203388281

+ 67 - 37
lib/calendar_controller/controller.dart

@@ -10,19 +10,47 @@ class CalendarController extends GetxController {
   int currentMonth = 12; // 当前月份
   int selectedDayIndex = 0; // 当前选择的日期的下标,从0开始,范围是0-34 有时是 0-41
   int maxDaysLength = 35; // 最大的日期数组长度,有时是42
-  List<DayStructure> daysList = []; // 当前维护的日期列表
-  ///TODO: 维护一个日程列表
+  List<Schedule> scheduleList = []; // 当前维护的日程列表
+  List<ScheduleType> scheduleTypeList = []; // 当前维护的一个日程类型列表
+  List<MiniViewDayStructure> miniViewDaysList = []; // 当前维护的迷你日历列表
+  /// 日程列表与日期列表拼装,即可变成月视图大列表,将日程塞入每一天
+  List<MonthViewDayStructure> get monthViewDaysList {
+    final List<MonthViewDayStructure> monthViewDaysList = [];
+    for (int i = 0; i < maxDaysLength; i++) {
+      final MiniViewDayStructure miniViewDayStructure = miniViewDaysList[i];
+      final MonthViewDayStructure monthViewDayStructure = MonthViewDayStructure(
+        index: miniViewDayStructure.index,
+        date: miniViewDayStructure.date,
+        isToday: miniViewDayStructure.isToday,
+        isCurrentMonth: miniViewDayStructure.isCurrentMonth,
+        isSelected: miniViewDayStructure.isSelected,
+        scheduleList: [],
+      );
+      monthViewDaysList.add(monthViewDayStructure);
+    }
+    for (final Schedule schedule in scheduleList) {
+      final DateTime scheduleDate = schedule.day;
+      final int scheduleDay = scheduleDate.day;
+      for (int i = 0; i < maxDaysLength; i++) {
+        final MiniViewDayStructure miniViewDayStructure = miniViewDaysList[i];
+        final MonthViewDayStructure monthViewDayStructure =
+            monthViewDaysList[i];
+        if (miniViewDayStructure.isCurrentMonth &&
+            miniViewDayStructure.date.day == scheduleDay) {
+          monthViewDayStructure.scheduleList.add(schedule);
+        }
+      }
+    }
+    return monthViewDaysList;
+  }
 
   /// TODO[Gavin]: i18n
   String get currentYearMonth => '$currentYear年$currentMonth月';
-  bool get isToday => daysList[selectedDayIndex].isToday;
+  bool get isToday => miniViewDaysList[selectedDayIndex].isToday;
   int get calendarLines => maxDaysLength ~/ 7;
-  // _initData() {
-  //   update(["calendar"]);
-  // }
-  FEventHandler<void> onDaysListChange = FEventHandler<void>();
 
-  void onTap() {}
+  /// 数据变更事件通知
+  FEventHandler<void> onDaysListChange = FEventHandler<void>();
 
   @override
   void onInit() {
@@ -32,7 +60,7 @@ class CalendarController extends GetxController {
     final DateTime now = DateTime.now();
     currentYear = now.year;
     currentMonth = now.month;
-    daysList = _countDaysList(currentYear, currentMonth);
+    miniViewDaysList = _countDaysList(currentYear, currentMonth);
     selectToday();
 
     /// 虚拟的日程数据
@@ -57,24 +85,26 @@ class CalendarController extends GetxController {
       DateTime(2022, 12, 30),
       DateTime(2022, 12, 31),
     ];
+
+    _initMockScheduleType();
+    _initMockSchedule(mockSchedule);
     _setSchedule(mockSchedule);
     print("CalendarController 全局临时控制器初始化");
   }
 
-  @override
-  void onReady() {
-    super.onReady();
-    // _initData();
+  void _initMockScheduleType() {
+    /// 创建5个假日程类型,填充到scheduleTypeList
+  }
+
+  /// TODO:[Gavin] 改为真实数据接口
+  void _initMockSchedule(List<DateTime> scheduleList) {
+    /// 创建30个假日程数据,填充到scheduleList
   }
 
-  // @override
-  // void onClose() {
-  //   super.onClose();
-  // }
   /// 传入起始年月,返回35个日期结构体
-  List<DayStructure> _countDaysList(int year, int month) {
+  List<MiniViewDayStructure> _countDaysList(int year, int month) {
     maxDaysLength = _isNeedExtraRow(year, month) ? 42 : 35;
-    final daysList = <DayStructure>[];
+    final miniViewDaysList = <MiniViewDayStructure>[];
     final firstDay = DateTime(year, month, 1);
     final firstDayWeek = firstDay.weekday;
     final lastDay = DateTime(year, month + 1, 0);
@@ -88,8 +118,8 @@ class CalendarController extends GetxController {
 
     // 上个月的日期
     for (var i = 0; i < firstDayWeekIndex; i++) {
-      daysList.add(
-        DayStructure(
+      miniViewDaysList.add(
+        MiniViewDayStructure(
           index: i,
           date: DateTime(
               year, month - 1, lastDayOfLastMonth - firstDayWeekIndex + i + 1),
@@ -100,10 +130,10 @@ class CalendarController extends GetxController {
 
     // 当月的日期
     for (var i = 0;
-        (i < lastDayOfMonth) && daysList.length < maxDaysLength;
+        (i < lastDayOfMonth) && miniViewDaysList.length < maxDaysLength;
         i++) {
-      daysList.add(
-        DayStructure(
+      miniViewDaysList.add(
+        MiniViewDayStructure(
           index: i + firstDayWeek,
           date: DateTime(year, month, i + 1),
           isToday:
@@ -112,16 +142,16 @@ class CalendarController extends GetxController {
       );
     }
 
-    while (daysList.length < maxDaysLength) {
-      daysList.add(DayStructure(
-        index: daysList.length,
+    while (miniViewDaysList.length < maxDaysLength) {
+      miniViewDaysList.add(MiniViewDayStructure(
+        index: miniViewDaysList.length,
         date: DateTime(month == 12 ? year + 1 : year,
             month == 12 ? 1 : month + 1, nextMonthDay),
         isCurrentMonth: false,
       ));
       nextMonthDay++;
     }
-    return daysList;
+    return miniViewDaysList;
   }
 
   /// 计算是否需要显示六行日期,一般为5行,但是有时候会有6行
@@ -140,9 +170,9 @@ class CalendarController extends GetxController {
   void handleSelectedDayByIndex(int index) {
     assert(index >= 0 && index < maxDaysLength,
         'index must be in 0-$maxDaysLength');
-    assert(daysList.length == maxDaysLength,
-        'daysList length must be maxDaysLength');
-    for (var element in daysList) {
+    assert(miniViewDaysList.length == maxDaysLength,
+        'miniViewDaysList length must be maxDaysLength');
+    for (var element in miniViewDaysList) {
       if (element.index == index) {
         element.isSelected = true;
       } else {
@@ -155,7 +185,7 @@ class CalendarController extends GetxController {
 
   /// 选中当月第一天
   void _selectFirstDay() {
-    for (var element in daysList) {
+    for (var element in miniViewDaysList) {
       if (element.isCurrentMonth) {
         handleSelectedDayByIndex(element.index);
         break;
@@ -170,9 +200,9 @@ class CalendarController extends GetxController {
     if (currentYear != now.year || currentMonth != now.month) {
       currentYear = now.year;
       currentMonth = now.month;
-      daysList = _countDaysList(currentYear, currentMonth);
+      miniViewDaysList = _countDaysList(currentYear, currentMonth);
     }
-    for (var element in daysList) {
+    for (var element in miniViewDaysList) {
       if (element.isToday) {
         handleSelectedDayByIndex(element.index);
         break;
@@ -184,7 +214,7 @@ class CalendarController extends GetxController {
   void _setSchedule(List<DateTime> scheduleList) {
     /// 如果scheduleList中存在daysList中的日期,则设置hasSchedule为true
     for (var element in scheduleList) {
-      for (var day in daysList) {
+      for (var day in miniViewDaysList) {
         if (element.year == day.date.year &&
             element.month == day.date.month &&
             element.day == day.date.day) {
@@ -202,7 +232,7 @@ class CalendarController extends GetxController {
     } else {
       currentMonth--;
     }
-    daysList = _countDaysList(currentYear, currentMonth);
+    miniViewDaysList = _countDaysList(currentYear, currentMonth);
     _selectFirstDay();
 
     onDaysListChange.emit(this, null);
@@ -216,7 +246,7 @@ class CalendarController extends GetxController {
     } else {
       currentMonth++;
     }
-    daysList = _countDaysList(currentYear, currentMonth);
+    miniViewDaysList = _countDaysList(currentYear, currentMonth);
     _selectFirstDay();
 
     onDaysListChange.emit(this, null);

+ 6 - 6
lib/calendar_view/mini_calendar/mini_calendar.dart

@@ -13,13 +13,13 @@ class MiniCalendar extends StatefulWidget {
 
 class _MiniCalendarState extends State<MiniCalendar> {
   String currYearMonth = ''; // 当前年月
-  List<DayStructure> daysList = []; // 当前控制器的日期列表
+  List<MiniViewDayStructure> daysList = []; // 当前控制器的日期列表
   CalendarController calendarController = Get.find<CalendarController>();
   @override
   void initState() {
     super.initState();
     currYearMonth = calendarController.currentYearMonth;
-    daysList = calendarController.daysList;
+    daysList = calendarController.miniViewDaysList;
     calendarController.onDaysListChange.addListener(_onDaysListChange);
   }
 
@@ -32,7 +32,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
   void _onDaysListChange(_, e) {
     setState(() {
       currYearMonth = calendarController.currentYearMonth;
-      daysList = calendarController.daysList;
+      daysList = calendarController.miniViewDaysList;
     });
   }
 
@@ -157,7 +157,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
   /// 构建日历主体
   Widget _buildMiniCalendarDaysBody() {
     // 将35天分成5个周
-    final weeksInMonth = <List<DayStructure>>[];
+    final weeksInMonth = <List<MiniViewDayStructure>>[];
     for (var i = 0; i < daysList.length; i += 7) {
       weeksInMonth.add(daysList.sublist(i, i + 7));
     }
@@ -169,7 +169,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
   }
 
   /// 构建每一行(周)
-  Widget _buildMiniCalendarDayRow(List<DayStructure> sevenDays) {
+  Widget _buildMiniCalendarDayRow(List<MiniViewDayStructure> sevenDays) {
     return Container(
       height: 32,
       child: Row(
@@ -180,7 +180,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
   }
 
   /// 构建每一天
-  Widget _buildEachDay(DayStructure day) {
+  Widget _buildEachDay(MiniViewDayStructure day) {
     return MiniDayItem(
       dayData: day,
       onSelect: (v) => calendarController.handleSelectedDayByIndex(v),

+ 2 - 2
lib/calendar_view/mini_calendar/mini_day_item.dart

@@ -5,14 +5,14 @@ import 'package:flutter/services.dart';
 /// 小日历的每一格日期
 class MiniDayItem extends StatefulWidget {
   const MiniDayItem({super.key, required this.dayData, required this.onSelect});
-  final DayStructure dayData;
+  final MiniViewDayStructure dayData;
   final ValueChanged<int> onSelect;
   @override
   State<MiniDayItem> createState() => _MyWidgetState();
 }
 
 class _MyWidgetState extends State<MiniDayItem> {
-  late DayStructure _dayData;
+  late MiniViewDayStructure _dayData;
   late BoxDecoration containerDecoration;
   late TextStyle dayTextStyle;
 

+ 8 - 8
lib/calendar_view/month_calendar/month_calendar.dart

@@ -15,7 +15,7 @@ class MonthCalendar extends StatefulWidget {
 }
 
 class _MonthCalendarState extends State<MonthCalendar> {
-  List<DayStructure> daysList = []; // 当前控制器的日期列表
+  List<MonthViewDayStructure> monthViewDaysList = []; // 当前控制器的日期列表
   CalendarController calendarController = Get.find<CalendarController>();
   int _maxScheduleLines = 3; // 最大的日程行数【需要动态计算】
   double _viewHeight = 0; // 维护一个视口高度,如果视口高度不变,那么日程的高度不需要计算
@@ -37,7 +37,7 @@ class _MonthCalendarState extends State<MonthCalendar> {
         });
       });
     }
-    daysList = calendarController.daysList;
+    monthViewDaysList = calendarController.monthViewDaysList;
     calendarController.onDaysListChange.addListener(_onDaysListChange);
   }
 
@@ -49,7 +49,7 @@ class _MonthCalendarState extends State<MonthCalendar> {
 
   void _onDaysListChange(_, e) {
     setState(() {
-      daysList = calendarController.daysList;
+      monthViewDaysList = calendarController.monthViewDaysList;
     });
     _handleMaxScheduleLines();
   }
@@ -99,9 +99,9 @@ class _MonthCalendarState extends State<MonthCalendar> {
   /// 构建日历主体
   Widget _buildMonthCalendarDaysBody() {
     // 将35天分成5个周
-    final weeksInMonth = <List<DayStructure>>[];
-    for (var i = 0; i < daysList.length; i += 7) {
-      weeksInMonth.add(daysList.sublist(i, i + 7));
+    final weeksInMonth = <List<MonthViewDayStructure>>[];
+    for (var i = 0; i < monthViewDaysList.length; i += 7) {
+      weeksInMonth.add(monthViewDaysList.sublist(i, i + 7));
     }
     return SizedBox(
       child: Column(
@@ -113,7 +113,7 @@ class _MonthCalendarState extends State<MonthCalendar> {
   }
 
   /// 构建每一行(周)
-  Widget _buildMonthCalendarDayRow(List<DayStructure> sevenDays) {
+  Widget _buildMonthCalendarDayRow(List<MonthViewDayStructure> sevenDays) {
     return Expanded(
       child: Container(
         child: Row(
@@ -125,7 +125,7 @@ class _MonthCalendarState extends State<MonthCalendar> {
   }
 
   /// 构建每一天
-  Widget _buildEachDay(DayStructure day) {
+  Widget _buildEachDay(MonthViewDayStructure day) {
     return Expanded(
       child: Container(
         decoration: const BoxDecoration(

+ 6 - 3
lib/calendar_view/month_calendar/month_calendar_item.dart

@@ -10,7 +10,7 @@ class MonthDayItem extends StatefulWidget {
       required this.dayData,
       // required this.onSelect,
       required this.maxScheduleLines});
-  final DayStructure dayData;
+  final MonthViewDayStructure dayData;
   // final ValueChanged<int> onSelect;
   final int maxScheduleLines;
 
@@ -20,7 +20,7 @@ class MonthDayItem extends StatefulWidget {
 }
 
 class _MyWidgetState extends State<MonthDayItem> {
-  late DayStructure _dayData;
+  late MonthViewDayStructure _dayData;
   late BoxDecoration _dateContainerDecoration;
   BoxDecoration _gridContainerDecoration = const BoxDecoration(
     color: Colors.white,
@@ -142,7 +142,10 @@ class _MyWidgetState extends State<MonthDayItem> {
       // ignore: sized_box_for_whitespace
       child: Container(
         width: double.infinity,
-        child: ScheduleList(maxLines: widget.maxScheduleLines),
+        child: ScheduleList(
+          maxLines: widget.maxScheduleLines,
+          scheduleDataList: widget.dayData.scheduleList,
+        ),
       ),
     );
   }

+ 17 - 27
lib/calendar_view/month_calendar/schedule_list.dart

@@ -4,7 +4,11 @@ import 'package:calendar_view/utils/calendar_util.dart';
 import 'package:flutter/material.dart';
 
 class ScheduleList extends StatefulWidget {
-  const ScheduleList({Key? key, required this.maxLines}) : super(key: key);
+  const ScheduleList({
+    Key? key,
+    required this.maxLines,
+    required this.scheduleDataList,
+  }) : super(key: key);
 
   /// 可容纳的最大日程行数
   final int maxLines;
@@ -12,46 +16,31 @@ class ScheduleList extends StatefulWidget {
   /// 每行日程的高度
   final double itemHeight = 20;
 
+  /// 日程数据
+  final List<Schedule> scheduleDataList;
+
   @override
   ScheduleListState createState() => ScheduleListState();
 }
 
 class ScheduleListState extends State<ScheduleList> {
-  static final List<ScheduleType> _mockScheduleTypeList = [
-    ScheduleType(typeName: '工作', isSelected: true, color: Colors.blue),
-    ScheduleType(typeName: '加班', isSelected: false, color: Colors.red),
-    ScheduleType(typeName: '学习', isSelected: false, color: Colors.green),
-    ScheduleType(typeName: '生活', isSelected: false, color: Colors.orange),
-    ScheduleType(typeName: '核酸', isSelected: false, color: Colors.purple),
-    ScheduleType(typeName: '工作', isSelected: true, color: Colors.blue),
-    ScheduleType(typeName: '加班', isSelected: false, color: Colors.red),
-    ScheduleType(typeName: '学习', isSelected: false, color: Colors.green),
-    ScheduleType(typeName: '生活', isSelected: false, color: Colors.orange),
-    ScheduleType(typeName: '核酸', isSelected: false, color: Colors.purple),
-  ];
-  List<ScheduleType> _scheduleTypeList = [];
-
   /// 容纳的数量
   int _itemCount = 0;
 
   @override
   void initState() {
     super.initState();
-
-    /// 填充假数据
-    final int random = Random().nextInt(10);
-    _scheduleTypeList = _mockScheduleTypeList.sublist(0, random);
-    _itemCount = _scheduleTypeList.length > widget.maxLines
+    _itemCount = widget.scheduleDataList.length > widget.maxLines
         ? widget.maxLines
-        : _scheduleTypeList.length;
+        : widget.scheduleDataList.length;
   }
 
   @override
   void didUpdateWidget(ScheduleList oldWidget) {
     super.didUpdateWidget(oldWidget);
-    _itemCount = _scheduleTypeList.length > widget.maxLines
+    _itemCount = widget.scheduleDataList.length > widget.maxLines
         ? widget.maxLines
-        : _scheduleTypeList.length;
+        : widget.scheduleDataList.length;
   }
 
   @override
@@ -75,13 +64,14 @@ class ScheduleListState extends State<ScheduleList> {
   }
 
   Widget _buildScheduleTypeItem(int scheduleIndex) {
-    /// TODO: 判断如果构建的是展示的最后一项,但不是列表的最后一项,则需要添加点击事件,并显示“还有${_scheduleTypeList.length - _itemCount + 1}项...”
+    /// TODO: 判断如果构建的是展示的最后一项,但不是列表的最后一项,则需要添加点击事件,并显示“还有${widget.scheduleDataList.length - _itemCount + 1}项...”
     if (scheduleIndex == _itemCount - 1) {
-      if (_itemCount < _scheduleTypeList.length) {
-        return _buildMoreItemButton(_scheduleTypeList.length - _itemCount + 1);
+      if (_itemCount < widget.scheduleDataList.length) {
+        return _buildMoreItemButton(
+            widget.scheduleDataList.length - _itemCount + 1);
       }
     }
-    ScheduleType scheduleType = _scheduleTypeList[scheduleIndex];
+    ScheduleType scheduleType = widget.scheduleDataList[scheduleIndex].type;
     return Material(
       color: Colors.transparent,
       child: InkWell(

+ 48 - 4
lib/utils/calendar_util.dart

@@ -1,8 +1,7 @@
-
 import 'package:flutter/material.dart';
 
-/// 结构化的日期数据
-class DayStructure {
+/// 迷你日历的日期数据结构
+class MiniViewDayStructure {
   final int index; // 下标
   final DateTime date; // 日期
   final bool isToday; // 是否是当天
@@ -10,7 +9,7 @@ class DayStructure {
   bool hasSchedule; // 是否包含日程
   bool isSelected; // 是否选中
 
-  DayStructure({
+  MiniViewDayStructure({
     required this.index,
     required this.date,
     this.isToday = false,
@@ -28,6 +27,33 @@ class DayStructure {
   }
 }
 
+/// 月视图的日期数据结构
+class MonthViewDayStructure {
+  final int index; // 下标
+  final DateTime date; // 日期
+  final bool isToday; // 是否是当天
+  final bool isCurrentMonth; // 是否是当月
+  bool isSelected; // 是否选中
+  List<Schedule> scheduleList; // 日程列表
+
+  MonthViewDayStructure({
+    required this.index,
+    required this.date,
+    this.isToday = false,
+    this.isCurrentMonth = true,
+    this.isSelected = false,
+    this.scheduleList = const [],
+  });
+
+  setSelected() {
+    isSelected = true;
+  }
+
+  setUnselected() {
+    isSelected = false;
+  }
+}
+
 /// 结构化的日程类型数据:类型名称,类型是否选中,类型颜色
 class ScheduleType {
   final String typeName; // 类型名称
@@ -40,3 +66,21 @@ class ScheduleType {
     required this.color,
   });
 }
+
+class Schedule {
+  final String title; // 日程标题
+  final String content; // 日程内容
+  final DateTime day; // 日程日期
+  final ScheduleType type; // 日程类型
+  final DateTime? startTime; // 日程开始时间
+  final DateTime? endTime; // 日程结束时间
+
+  Schedule({
+    required this.title,
+    required this.content,
+    required this.day,
+    required this.type,
+    this.startTime,
+    this.endTime,
+  });
+}