日报周报数据紊乱

This commit is contained in:
czz
2025-10-16 10:29:46 +08:00
parent 6862b97e17
commit 5e93369dd0
2 changed files with 157 additions and 59 deletions

View File

@@ -15,6 +15,17 @@ Widget WeekDataWidget(
Map<dynamic, dynamic> sleepReport,
dynamic data,
) {
List<String> getDate() {
var s = buildWeekDatesAndPoints(sleepReport['scoreList']);
if (s == null ||
s['dates'] == null ||
s['dates'] is! List<String> ||
s['dates'].length != 7) {
return [];
}
return s['dates'];
}
List<Widget> _buildSectionList() {
EdgeInsetsDirectional padding =
EdgeInsetsDirectional.fromSTEB(30.rpx, 0, 30.rpx, 25.rpx);
@@ -34,8 +45,7 @@ Widget WeekDataWidget(
min: 0, //最小值0
max: 7, //最大值30
q: 6, //labels第一个与最后一个的真实距离也就是30-1 = 29
labels:
buildWeekDatesAndPoints(sleepReport['scoreList'])['dates'],
labels: getDate(),
indexs: [0, 1, 2, 3, 4, 5, 6], //每一个标签的对应在X轴的真实位置
offset: Offset(0, -16.rpx), //标签相对于原点的偏移下方20像素位置
ondrawer: (canvas, offset, index, label, align, style) {
@@ -766,8 +776,72 @@ int getWeekdayX(DateTime current, DateTime first) {
return weekOffset * 7 + (current.weekday - 1);
}
Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic> scoreList) {
if (!scoreList.containsKey('data') || (scoreList['data'] as List).isEmpty) {
// Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic> scoreList) {
// if (scoreList == null ||
// !scoreList.containsKey('data') ||
// (scoreList['data'] as List).isEmpty) {
// return {
// 'dates': [],
// 'points': [],
// 'colors': [],
// };
// }
// List<Map<String, dynamic>> data = (scoreList['data'] as List)
// .where((item) => item is Map<String, dynamic>)
// .cast<Map<String, dynamic>>()
// .toList();
// // 提取 level -> color 映射表
// Map<int, String> levelColorMap = {};
// if (scoreList.containsKey('type')) {
// List typeList = scoreList['type'];
// for (var item in typeList) {
// if (item is Map &&
// item.containsKey('level') &&
// item.containsKey('color')) {
// levelColorMap[item['level']] = item['color'];
// }
// }
// }
// DateTime baseDate = DateTime.fromMillisecondsSinceEpoch(data.first['st']);
// int weekday = baseDate.weekday;
// DateTime monday = baseDate.subtract(Duration(days: weekday - 1));
// List<String> dates = List.generate(7, (i) {
// DateTime d = monday.add(Duration(days: i));
// String month = d.month.toString().padLeft(2, '0');
// String day = d.day.toString().padLeft(2, '0');
// return "$month/$day";
// });
// List<Offset> points = [];
// List<String> colors = [];
// for (var item in data) {
// DateTime dt = DateTime.fromMillisecondsSinceEpoch(item['st']);
// double x = getWeekdayX(dt, baseDate).toDouble();
// double y = (item['value'] as num?)?.toDouble() ?? 0;
// int level = item['level'];
// String color = levelColorMap[level] ?? "#FFFF00";
// points.add(Offset(x, y));
// colors.add(color);
// }
// return {
// 'dates': dates,
// 'points': points,
// 'colors': colors,
// };
// }
Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic>? scoreList) {
try {
if (scoreList == null ||
!scoreList.containsKey('data') ||
(scoreList['data'] as List).isEmpty) {
return {
'dates': [],
'points': [],
@@ -780,6 +854,14 @@ Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic> scoreList) {
.cast<Map<String, dynamic>>()
.toList();
if (data.isEmpty) {
return {
'dates': [],
'points': [],
'colors': [],
};
}
// 提取 level -> color 映射表
Map<int, String> levelColorMap = {};
if (scoreList.containsKey('type')) {
@@ -793,9 +875,15 @@ Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic> scoreList) {
}
}
DateTime baseDate = DateTime.fromMillisecondsSinceEpoch(data.first['st']);
// 如果 st 字段异常,使用当前时间作为基准
int stValue = (data.first['st'] is int)
? data.first['st']
: DateTime.now().millisecondsSinceEpoch;
DateTime baseDate = DateTime.fromMillisecondsSinceEpoch(stValue);
int weekday = baseDate.weekday;
DateTime monday = baseDate.subtract(Duration(days: weekday - 1));
List<String> dates = List.generate(7, (i) {
DateTime d = monday.add(Duration(days: i));
String month = d.month.toString().padLeft(2, '0');
@@ -807,10 +895,12 @@ Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic> scoreList) {
List<String> colors = [];
for (var item in data) {
if (item['st'] == null) continue;
DateTime dt = DateTime.fromMillisecondsSinceEpoch(item['st']);
double x = getWeekdayX(dt, baseDate).toDouble();
double y = (item['value'] as num?)?.toDouble() ?? 0;
int level = item['level'];
int level = (item['level'] is int) ? item['level'] : 0;
String color = levelColorMap[level] ?? "#FFFF00";
points.add(Offset(x, y));
@@ -822,6 +912,18 @@ Map<String, dynamic> buildWeekDatesAndPoints(Map<String, dynamic> scoreList) {
'points': points,
'colors': colors,
};
} catch (e, s) {
// ⚠️ 捕获异常并返回空结构
debugPrint("❌ buildWeekDatesAndPoints 出错:$e");
debugPrint("—— 异常堆栈 ——");
debugPrintStack(stackTrace: s);
debugPrint("———————————");
return {
'dates': [],
'points': [],
'colors': [],
};
}
}
List<Offset> buildGeneralPoints(Map<String, dynamic> dyspData) {

View File

@@ -58,6 +58,7 @@ class _NewSleepReportPageState extends State<NewSleepReportPage> {
Future.microtask(() {
if (Get.isRegistered<SleepReportController>()) {
sleepReportController.isLoading.value = false;
sleepReportController.model.type = 1;
sleepReportController.sleepReport.value = {};
}
});
@@ -876,7 +877,7 @@ class _NewSleepReportPageState extends State<NewSleepReportPage> {
// }
// }),
Obx(() {
try {
if (sleepReportController.isLoading.value) {
return Center(
child: CircularProgressIndicator(
@@ -908,12 +909,7 @@ class _NewSleepReportPageState extends State<NewSleepReportPage> {
default:
return NullDataWidget();
}
} catch (e, s) {
debugPrint("❌ Obx 构建异常: $e");
debugPrintStack(
stackTrace: s, label: "Obx build 详细堆栈:");
return NullDataWidget();
}
}),
Column(
children: [