|
@@ -1,10 +1,16 @@
|
|
|
-import 'dart:html';
|
|
|
+import 'dart:math';
|
|
|
+
|
|
|
import 'package:calendar_view/calendar_view/calendar_util.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
-import 'package:calendar_view/utils/throttle.dart' as utils;
|
|
|
|
|
|
class ScheduleList extends StatefulWidget {
|
|
|
- const ScheduleList({Key? key}) : super(key: key);
|
|
|
+ const ScheduleList({Key? key, required this.maxLines}) : super(key: key);
|
|
|
+
|
|
|
+ /// 可容纳的最大日程行数
|
|
|
+ final int maxLines;
|
|
|
+
|
|
|
+ /// 每行日程的高度
|
|
|
+ final double itemHeight = 20;
|
|
|
|
|
|
@override
|
|
|
ScheduleListState createState() => ScheduleListState();
|
|
@@ -17,53 +23,35 @@ class ScheduleListState extends State<ScheduleList> {
|
|
|
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),
|
|
|
];
|
|
|
- static List<ScheduleType> _scheduleTypeList = [];
|
|
|
-
|
|
|
- /// 容器高度【动态】
|
|
|
- double conatinerHeight = 0;
|
|
|
-
|
|
|
- /// 每个item的高度
|
|
|
- static const double _itemHeight = 20;
|
|
|
+ List<ScheduleType> _scheduleTypeList = [];
|
|
|
|
|
|
/// 容纳的数量
|
|
|
- int itemCount = 0;
|
|
|
+ int _itemCount = 0;
|
|
|
|
|
|
@override
|
|
|
void initState() {
|
|
|
super.initState();
|
|
|
- _scheduleTypeList = _mockScheduleTypeList;
|
|
|
- window.onResize.listen(((event) => {
|
|
|
- utils.throttle(() {
|
|
|
- _onWindowResize();
|
|
|
- }, 'onResize_${widget.hashCode}', 500)
|
|
|
- }));
|
|
|
- WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
|
|
- _onWindowResize();
|
|
|
- });
|
|
|
- }
|
|
|
|
|
|
- void _onWindowResize() {
|
|
|
- setState(() {
|
|
|
- conatinerHeight = context.size!.height;
|
|
|
- itemCount = _countItemNum();
|
|
|
- });
|
|
|
- _scheduleTypeList = _mockScheduleTypeList;
|
|
|
- // 如果缩减了数量,则将最后一项显示的内容设置为更多
|
|
|
- if (itemCount < _scheduleTypeList.length) {
|
|
|
- _scheduleTypeList[itemCount - 1] = ScheduleType(
|
|
|
- typeName: '还有${_scheduleTypeList.length - itemCount + 1}项...',
|
|
|
- isSelected: false,
|
|
|
- color: Colors.grey);
|
|
|
- }
|
|
|
+ /// 填充假数据
|
|
|
+ final int random = Random().nextInt(10);
|
|
|
+ _scheduleTypeList = _mockScheduleTypeList.sublist(0, random);
|
|
|
+ _itemCount = _scheduleTypeList.length > widget.maxLines
|
|
|
+ ? widget.maxLines
|
|
|
+ : _scheduleTypeList.length;
|
|
|
}
|
|
|
|
|
|
- /// 计算可容纳的item数量
|
|
|
- int _countItemNum() {
|
|
|
- final maxNum = (conatinerHeight / _itemHeight).floor();
|
|
|
- return maxNum > _scheduleTypeList.length
|
|
|
- ? _scheduleTypeList.length
|
|
|
- : maxNum;
|
|
|
+ @override
|
|
|
+ void didUpdateWidget(ScheduleList oldWidget) {
|
|
|
+ super.didUpdateWidget(oldWidget);
|
|
|
+ _itemCount = _scheduleTypeList.length > widget.maxLines
|
|
|
+ ? widget.maxLines
|
|
|
+ : _scheduleTypeList.length;
|
|
|
}
|
|
|
|
|
|
@override
|
|
@@ -75,16 +63,26 @@ class ScheduleListState extends State<ScheduleList> {
|
|
|
|
|
|
Widget _buildScheduleTypeList() {
|
|
|
return ListView.builder(
|
|
|
- itemCount: itemCount,
|
|
|
+ addAutomaticKeepAlives: false,
|
|
|
+ addRepaintBoundaries: true,
|
|
|
+ itemExtent: widget.itemHeight,
|
|
|
+ itemCount: _itemCount,
|
|
|
itemBuilder: (context, index) {
|
|
|
- return _buildScheduleTypeItem(_scheduleTypeList[index]);
|
|
|
+ return _buildScheduleTypeItem(index);
|
|
|
},
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- Widget _buildScheduleTypeItem(ScheduleType scheduleType) {
|
|
|
+ Widget _buildScheduleTypeItem(int scheduleIndex) {
|
|
|
+ /// TODO: 判断如果构建的是展示的最后一项,但不是列表的最后一项,则需要添加点击事件,并显示“还有${_scheduleTypeList.length - _itemCount + 1}项...”
|
|
|
+ if (scheduleIndex == _itemCount - 1) {
|
|
|
+ if (_itemCount < _scheduleTypeList.length) {
|
|
|
+ return _buildMoreItemButton(_scheduleTypeList.length - _itemCount + 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ScheduleType scheduleType = _scheduleTypeList[scheduleIndex];
|
|
|
return Container(
|
|
|
- height: _itemHeight,
|
|
|
+ height: widget.itemHeight,
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
|
|
|
color: Colors.transparent,
|
|
|
child: Row(
|
|
@@ -106,4 +104,29 @@ class ScheduleListState extends State<ScheduleList> {
|
|
|
),
|
|
|
);
|
|
|
}
|
|
|
+
|
|
|
+ Widget _buildMoreItemButton(int moreNum) {
|
|
|
+ return Container(
|
|
|
+ height: widget.itemHeight,
|
|
|
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
|
|
|
+ color: Colors.transparent,
|
|
|
+ child: Row(
|
|
|
+ children: [
|
|
|
+ Container(
|
|
|
+ width: 6,
|
|
|
+ height: 6,
|
|
|
+ decoration: BoxDecoration(
|
|
|
+ color: Colors.transparent,
|
|
|
+ borderRadius: BorderRadius.circular(5),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ const SizedBox(width: 10),
|
|
|
+ Text(
|
|
|
+ '还有$moreNum项...',
|
|
|
+ style: const TextStyle(fontSize: 12),
|
|
|
+ ),
|
|
|
+ ],
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|