Files
tuiche/lib/component/base/SleepdateWidget.dart
2025-07-10 11:16:26 +08:00

130 lines
3.9 KiB
Dart

import 'package:flutter/material.dart';
import 'package:vbvs_app/common/util/FitTool.dart';
import 'package:vbvs_app/common/util/MyUtils.dart';
import 'package:vbvs_app/component/tool/ClickableContainer.dart';
class SleepdateWidget extends StatelessWidget {
final DateTime date;
final bool isSelected;
final VoidCallback onTap;
final Color highlightColor; // 新增
final Map sleepDate;
SleepdateWidget({
required this.sleepDate,
required this.date,
required this.isSelected,
required this.onTap,
this.highlightColor = Colors.black, // 默认值黑色
});
@override
// Widget build(BuildContext context) {
// return ClickableContainer(
// onTap: onTap,
// backgroundColor: Colors.transparent,
// highlightColor: Colors.transparent,
// padding: EdgeInsets.all(4.rpx),
// child: Container(
// width: 90.rpx,
// height: 90.rpx,
// decoration: BoxDecoration(
// borderRadius: BorderRadius.circular(30.rpx),
// color: isSelected ? highlightColor : Colors.transparent, // 使用传入的颜色
// ),
// child: Padding(
// padding:
// EdgeInsetsDirectional.fromSTEB(10.rpx, 10.rpx, 10.rpx, 10.rpx),
// child: Container(
// decoration: BoxDecoration(
// color: Color(0xFF757575),
// shape: BoxShape.circle,
// ),
// alignment: Alignment.center,
// child: Text(
// '${date.day}',
// style: TextStyle(
// color: Colors.white,
// fontSize: 26.rpx,
// letterSpacing: 0.0,
// ),
// ),
// ),
// ),
// ),
// );
// }
@override
Widget build(BuildContext context) {
Color? fillColor;
// 判断是否存在 score 数据
final List<dynamic>? dataList = sleepDate['scoreList']?['data'];
final List<dynamic>? typeList = sleepDate['scoreList']?['type'];
if (dataList != null && typeList != null) {
// 查找是否有匹配日期的数据
for (var item in dataList) {
final st = item['st'];
final level = item['level'];
if (st is int) {
final itemDate =
DateTime.fromMillisecondsSinceEpoch(st).toLocal(); // 转为本地时间
// 判断是否是同一天
if (itemDate.year == date.year &&
itemDate.month == date.month &&
itemDate.day == date.day) {
// 找到对应 level 的颜色
final matchType = typeList.firstWhere(
(e) => e['level'] == level,
orElse: () => null,
);
if (matchType != null && matchType['color'] != null) {
final hexColor = matchType['color'];
fillColor = stringToColor(hexColor);
}
break; // 找到就跳出
}
}
}
}
return ClickableContainer(
onTap: onTap,
backgroundColor: Colors.transparent,
highlightColor: Colors.transparent,
padding: EdgeInsets.all(4.rpx),
child: Container(
width: 90.rpx,
height: 90.rpx,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.rpx),
color: isSelected ? highlightColor : Colors.transparent,
),
child: Padding(
padding:
EdgeInsetsDirectional.fromSTEB(10.rpx, 10.rpx, 10.rpx, 10.rpx),
child: Container(
decoration: BoxDecoration(
color: fillColor ?? Color(0xFF757575), // 如果匹配不到就默认灰色
shape: BoxShape.circle,
),
alignment: Alignment.center,
child: Text(
'${date.day}',
style: TextStyle(
color: Colors.white,
fontSize: 26.rpx,
letterSpacing: 0.0,
),
),
),
),
),
);
}
}