|
@@ -0,0 +1,109 @@
|
|
|
+import 'dart:html';
|
|
|
+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);
|
|
|
+
|
|
|
+ @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),
|
|
|
+ ];
|
|
|
+ static List<ScheduleType> _scheduleTypeList = [];
|
|
|
+
|
|
|
+ /// 容器高度【动态】
|
|
|
+ double conatinerHeight = 0;
|
|
|
+
|
|
|
+ /// 每个item的高度
|
|
|
+ static const double _itemHeight = 20;
|
|
|
+
|
|
|
+ /// 容纳的数量
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 计算可容纳的item数量
|
|
|
+ int _countItemNum() {
|
|
|
+ final maxNum = (conatinerHeight / _itemHeight).floor();
|
|
|
+ return maxNum > _scheduleTypeList.length
|
|
|
+ ? _scheduleTypeList.length
|
|
|
+ : maxNum;
|
|
|
+ }
|
|
|
+
|
|
|
+ @override
|
|
|
+ Widget build(BuildContext context) {
|
|
|
+ return Container(
|
|
|
+ child: _buildScheduleTypeList(),
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ Widget _buildScheduleTypeList() {
|
|
|
+ return ListView.builder(
|
|
|
+ itemCount: itemCount,
|
|
|
+ itemBuilder: (context, index) {
|
|
|
+ return _buildScheduleTypeItem(_scheduleTypeList[index]);
|
|
|
+ },
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ Widget _buildScheduleTypeItem(ScheduleType scheduleType) {
|
|
|
+ return Container(
|
|
|
+ height: _itemHeight,
|
|
|
+ padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
|
|
|
+ color: Colors.transparent,
|
|
|
+ child: Row(
|
|
|
+ children: [
|
|
|
+ Container(
|
|
|
+ width: 6,
|
|
|
+ height: 6,
|
|
|
+ decoration: BoxDecoration(
|
|
|
+ color: scheduleType.color,
|
|
|
+ borderRadius: BorderRadius.circular(5),
|
|
|
+ ),
|
|
|
+ ),
|
|
|
+ const SizedBox(width: 10),
|
|
|
+ Text(
|
|
|
+ scheduleType.typeName,
|
|
|
+ style: const TextStyle(fontSize: 12),
|
|
|
+ ),
|
|
|
+ ],
|
|
|
+ ),
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|