Parcourir la source

日程组件拆分为有状态组件

gavin.chen il y a 2 ans
Parent
commit
4f0b71e83b

+ 1 - 2
lib/calendar_page/calendar_left_panel.dart

@@ -40,9 +40,8 @@ class _CalendarLeftPanelState extends State<CalendarLeftPanel> {
 
   /// 构建顶部【搜索日程、添加日程】
   Widget _buildPanelTop() {
-    return Container(
+    return SizedBox(
       height: 70,
-      // color: const Color.fromARGB(90, 222, 59, 255),
       child: Container(
         padding: const EdgeInsets.symmetric(horizontal: 10),
         child: Row(

+ 1 - 2
lib/calendar_page/calendar_main_panel.dart

@@ -2,7 +2,6 @@ import 'package:calendar_view/calendar_controller/controller.dart';
 import 'package:calendar_view/calendar_page/calendar_views/calendar_month_view.dart';
 import 'package:calendar_view/calendar_page/my_calendar/my_calendar.dart';
 import 'package:calendar_view/popup_layer/popup_layer.dart';
-import 'package:calendar_view/popup_layer/popup_layer_controller.dart';
 import 'package:flutter/material.dart';
 import 'package:get/get.dart';
 
@@ -85,7 +84,7 @@ class _CalendarMainPanelState extends State<CalendarMainPanel> {
 
   /// 构建顶部【今天、年月、视图切换器、更多】
   Widget _buildMainPanelTop() {
-    return Container(
+    return SizedBox(
       height: 70,
       child: Container(
         padding: const EdgeInsets.symmetric(horizontal: 10),

+ 3 - 3
lib/calendar_page/mini_calendar/mini_calendar.dart

@@ -109,7 +109,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
 
   Widget _buildMiniCalendarBody() {
     return Container(
-      padding: EdgeInsets.symmetric(horizontal: 5),
+      padding: const EdgeInsets.symmetric(horizontal: 5),
       child: Column(
         children: <Widget>[
           _buildMiniCalendarWeekTitle(),
@@ -124,7 +124,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
 
   /// 构建「日、一、二、三、四、五、六」的标题
   Widget _buildMiniCalendarWeekTitle() {
-    return Container(
+    return SizedBox(
       height: 30,
       child: Row(
         mainAxisAlignment: MainAxisAlignment.spaceBetween,
@@ -170,7 +170,7 @@ class _MiniCalendarState extends State<MiniCalendar> {
 
   /// 构建每一行(周)
   Widget _buildMiniCalendarDayRow(List<MiniViewDayStructure> sevenDays) {
-    return Container(
+    return SizedBox(
       height: 32,
       child: Row(
         mainAxisAlignment: MainAxisAlignment.spaceBetween,

+ 4 - 7
lib/calendar_page/month_calendar/month_calendar.dart

@@ -66,7 +66,7 @@ class _MonthCalendarState extends State<MonthCalendar> {
 
   /// 构建「日、一、二、三、四、五、六」的标题
   Widget _buildMonthCalendarWeekTitle() {
-    return Container(
+    return SizedBox(
       height: 30,
       child: Row(
         mainAxisAlignment: MainAxisAlignment.spaceAround,
@@ -115,11 +115,9 @@ class _MonthCalendarState extends State<MonthCalendar> {
   /// 构建每一行(周)
   Widget _buildMonthCalendarDayRow(List<MonthViewDayStructure> sevenDays) {
     return Expanded(
-      child: Container(
-        child: Row(
-          mainAxisAlignment: MainAxisAlignment.spaceAround,
-          children: sevenDays.map(_buildEachDay).toList(),
-        ),
+      child: Row(
+        mainAxisAlignment: MainAxisAlignment.spaceAround,
+        children: sevenDays.map(_buildEachDay).toList(),
       ),
     );
   }
@@ -153,7 +151,6 @@ class _MonthCalendarState extends State<MonthCalendar> {
     final size = MediaQuery.of(context).size;
     if (size.height == _viewHeight &&
         calendarController.calendarLines == _calendarLines) return;
-    print('计算最大日程行数');
     _viewHeight = size.height;
     _calendarLines = calendarController.calendarLines;
     final gridRows = calendarController.calendarLines;

+ 28 - 72
lib/calendar_page/month_calendar/more_schedule_popup.dart

@@ -1,4 +1,5 @@
 import 'package:calendar_view/calendar_controller/controller.dart';
+import 'package:calendar_view/calendar_page/month_calendar/schedule_item.dart';
 import 'package:calendar_view/utils/calendar_util.dart';
 import 'package:calendar_view/utils/chinese_calendar_utils.dart';
 import 'package:flutter/material.dart';
@@ -29,17 +30,29 @@ class MoreSchedulePopupState extends State<MoreSchedulePopup> {
         calendarController.scheduleListFilter(widget.scheduleData.scheduleList);
   }
 
+  @override
+  void didUpdateWidget(covariant MoreSchedulePopup oldWidget) {
+    super.didUpdateWidget(oldWidget);
+    _scheduleDataList =
+        calendarController.scheduleListFilter(widget.scheduleData.scheduleList);
+  }
+
   @override
   Widget build(BuildContext context) {
-    return Container(
-      padding: const EdgeInsets.all(10),
-      child: Column(
-        mainAxisSize: MainAxisSize.min,
-        children: [
-          _buildScheduleHead(),
-          _buildScheduleTypeList(),
-        ],
-      ),
+    return ListView(
+      shrinkWrap: true,
+      children: [
+        Container(
+          padding: const EdgeInsets.all(10),
+          child: Column(
+            mainAxisSize: MainAxisSize.min,
+            children: [
+              _buildScheduleHead(),
+              _buildScheduleList(),
+            ],
+          ),
+        ),
+      ],
     );
   }
 
@@ -67,7 +80,7 @@ class MoreSchedulePopupState extends State<MoreSchedulePopup> {
     );
   }
 
-  Widget _buildScheduleTypeList() {
+  Widget _buildScheduleList() {
     return ListView.builder(
       shrinkWrap: true,
       addAutomaticKeepAlives: false,
@@ -75,76 +88,19 @@ class MoreSchedulePopupState extends State<MoreSchedulePopup> {
       itemExtent: _itemHeight,
       itemCount: _scheduleDataList.length,
       itemBuilder: (context, index) {
-        return _buildScheduleTypeItem(index);
+        return _buildScheduleItem(index);
       },
     );
   }
 
-  Widget _buildScheduleTypeItem(int scheduleIndex) {
-    ScheduleType scheduleType = _scheduleDataList[scheduleIndex].type;
+  Widget _buildScheduleItem(int scheduleIndex) {
     Schedule schedule = _scheduleDataList[scheduleIndex];
-    return Material(
-      color: Colors.transparent,
-      child: InkWell(
-        customBorder: RoundedRectangleBorder(
-          borderRadius: BorderRadius.circular(5),
-        ),
-        onTap: () {
-          ScaffoldMessenger.of(context).showSnackBar(
-            SnackBar(
-              backgroundColor: scheduleType.color,
-              duration: const Duration(milliseconds: 500),
-              content: Text('点击了${scheduleType.typeName}'),
-            ),
-          );
-        },
-        hoverColor: const Color.fromARGB(255, 227, 228, 228),
-        child: Container(
-          height: _itemHeight,
-          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
-          decoration: BoxDecoration(
-            // color: scheduleType.isSelected
-            //     ? scheduleType.color.withOpacity(0.2)
-            //     : Colors.transparent,
-            borderRadius: BorderRadius.circular(5),
-          ),
-          child: Row(
-            children: [
-              Container(
-                width: 6,
-                height: 6,
-                decoration: BoxDecoration(
-                  color: scheduleType.color,
-                  borderRadius: BorderRadius.circular(5),
-                ),
-              ),
-              const SizedBox(width: 5),
-              Expanded(
-                child: Text(
-                  breakWord(schedule.title),
-                  style: const TextStyle(fontSize: 12),
-                  overflow: TextOverflow.ellipsis,
-                ),
-              ),
-            ],
-          ),
-        ),
-      ),
+    return SchedualItem(
+      scheduleData: schedule,
+      itemHeight: _itemHeight,
     );
   }
 
-  static String breakWord(String text) {
-    if (text.isEmpty) {
-      return text;
-    }
-    String breakWord = ' ';
-    for (var element in text.runes) {
-      breakWord += String.fromCharCode(element);
-      breakWord += '\u200B';
-    }
-    return breakWord;
-  }
-
   String _getLunarDay(DateTime date) {
     CalendarInfo lunarDate = CalendarUtils.getInfo(date);
     return lunarDate.term ?? lunarDate.lunarDayName ?? '';

+ 80 - 0
lib/calendar_page/month_calendar/schedule_item.dart

@@ -0,0 +1,80 @@
+import 'package:calendar_view/utils/calendar_util.dart';
+import 'package:flutter/material.dart';
+
+class SchedualItem extends StatefulWidget {
+  const SchedualItem(
+      {super.key, required this.scheduleData, required this.itemHeight});
+  final Schedule scheduleData;
+  final double itemHeight;
+
+  @override
+  State<SchedualItem> createState() => _SchedualItemState();
+}
+
+class _SchedualItemState extends State<SchedualItem> {
+  get scheduleType => widget.scheduleData.type;
+  get schedule => widget.scheduleData;
+  @override
+  Widget build(BuildContext context) {
+    return Material(
+      color: Colors.transparent,
+      child: InkWell(
+        customBorder: RoundedRectangleBorder(
+          borderRadius: BorderRadius.circular(5),
+        ),
+        onTap: () {
+          ScaffoldMessenger.of(context).showSnackBar(
+            SnackBar(
+              backgroundColor: scheduleType.color,
+              duration: const Duration(milliseconds: 500),
+              content: Text('点击了${scheduleType.typeName}'),
+            ),
+          );
+        },
+        hoverColor: const Color.fromARGB(255, 227, 228, 228),
+        child: Container(
+          height: widget.itemHeight,
+          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
+          decoration: BoxDecoration(
+            // color: scheduleType.isSelected
+            //     ? scheduleType.color.withOpacity(0.2)
+            //     : Colors.transparent,
+            borderRadius: BorderRadius.circular(5),
+          ),
+          child: Row(
+            children: [
+              Container(
+                width: 6,
+                height: 6,
+                decoration: BoxDecoration(
+                  color: scheduleType.color,
+                  borderRadius: BorderRadius.circular(5),
+                ),
+              ),
+              const SizedBox(width: 5),
+              Expanded(
+                child: Text(
+                  breakWord(schedule.title),
+                  style: const TextStyle(fontSize: 12),
+                  overflow: TextOverflow.ellipsis,
+                ),
+              ),
+            ],
+          ),
+        ),
+      ),
+    );
+  }
+
+  static String breakWord(String text) {
+    if (text.isEmpty) {
+      return text;
+    }
+    String breakWord = ' ';
+    for (var element in text.runes) {
+      breakWord += String.fromCharCode(element);
+      breakWord += '\u200B';
+    }
+    return breakWord;
+  }
+}

+ 7 - 53
lib/calendar_page/month_calendar/schedule_list.dart

@@ -1,7 +1,6 @@
-import 'package:calendar_view/popup_layer/popup_layer_controller.dart';
+import 'package:calendar_view/calendar_page/month_calendar/schedule_item.dart';
 import 'package:calendar_view/utils/calendar_util.dart';
 import 'package:flutter/material.dart';
-import 'package:get/get.dart';
 
 class ScheduleList extends StatefulWidget {
   const ScheduleList({
@@ -67,65 +66,20 @@ class ScheduleListState extends State<ScheduleList> {
   }
 
   Widget _buildScheduleTypeItem(int scheduleIndex) {
-    /// TODO: 判断如果构建的是展示的最后一项,但不是列表的最后一项,则需要添加点击事件,并显示“还有${widget.scheduleDataList.length - _itemCount + 1}项...”
     if (scheduleIndex == _itemCount - 1) {
       if (_itemCount < widget.scheduleDataList.length) {
         return _buildMoreItemButton(
             widget.scheduleDataList.length - _itemCount + 1);
       }
     }
-    ScheduleType scheduleType = widget.scheduleDataList[scheduleIndex].type;
     Schedule schedule = widget.scheduleDataList[scheduleIndex];
-    return Material(
-      color: Colors.transparent,
-      child: InkWell(
-        customBorder: RoundedRectangleBorder(
-          borderRadius: BorderRadius.circular(5),
-        ),
-        onTap: () {
-          ScaffoldMessenger.of(context).showSnackBar(
-            SnackBar(
-              backgroundColor: scheduleType.color,
-              duration: const Duration(milliseconds: 500),
-              content: Text('点击了${scheduleType.typeName}'),
-            ),
-          );
-        },
-        hoverColor: const Color.fromARGB(255, 227, 228, 228),
-        child: Container(
-          height: widget.itemHeight,
-          padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
-          decoration: BoxDecoration(
-            // color: scheduleType.isSelected
-            //     ? scheduleType.color.withOpacity(0.2)
-            //     : Colors.transparent,
-            borderRadius: BorderRadius.circular(5),
-          ),
-          child: Row(
-            children: [
-              Container(
-                width: 6,
-                height: 6,
-                decoration: BoxDecoration(
-                  color: scheduleType.color,
-                  borderRadius: BorderRadius.circular(5),
-                ),
-              ),
-              const SizedBox(width: 5),
-              Expanded(
-                child: Text(
-                  breakWord(schedule.title),
-                  style: const TextStyle(fontSize: 12),
-                  overflow: TextOverflow.ellipsis,
-                ),
-              ),
-            ],
-          ),
-        ),
-      ),
+    return SchedualItem(
+      scheduleData: schedule,
+      itemHeight: widget.itemHeight,
     );
   }
 
+  /// 构建【显示更多】按钮
   Widget _buildMoreItemButton(int moreNum) {
     return Material(
       color: Colors.transparent,
@@ -174,10 +128,10 @@ class ScheduleListState extends State<ScheduleList> {
       return text;
     }
     String breakWord = ' ';
-    text.runes.forEach((element) {
+    for (var element in text.runes) {
       breakWord += String.fromCharCode(element);
       breakWord += '\u200B';
-    });
+    }
     return breakWord;
   }
 }

+ 6 - 8
lib/calendar_page/my_calendar/my_calendar.dart

@@ -39,14 +39,12 @@ class _MyCalendarState extends State<MyCalendar> {
 
   @override
   Widget build(BuildContext context) {
-    return Container(
-      child: ListView(
-        shrinkWrap: true,
-        children: <Widget>[
-          _buildMyCalendarTitle(),
-          if (_isExpanded) ..._scheduleTypeList.map(_buildMyCalendarItem),
-        ],
-      ),
+    return ListView(
+      shrinkWrap: true,
+      children: <Widget>[
+        _buildMyCalendarTitle(),
+        if (_isExpanded) ..._scheduleTypeList.map(_buildMyCalendarItem),
+      ],
     );
   }
 

+ 0 - 5
lib/popup_layer/popup_layer_controller.dart

@@ -24,9 +24,4 @@ class PopupLayerController extends GetxController {
     currMoreScheduleData = moreScheduleData;
     onPopupMoreSchedule.emit(this, key);
   }
-
-  @override
-  void onInit() {
-    super.onInit();
-  }
 }