From 5e93369dd05d242e590c4c6f80516b399a11e8e2 Mon Sep 17 00:00:00 2001 From: czz <862977248@qq.com> Date: Thu, 16 Oct 2025 10:29:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E6=8A=A5=E5=91=A8=E6=8A=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=B4=8A=E4=B9=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../component/WeekDataWidget.dart | 206 +++++++++++++----- .../sleep_report/new_sleep_report_page.dart | 10 +- 2 files changed, 157 insertions(+), 59 deletions(-) diff --git a/lib/pages/sleep_report/component/WeekDataWidget.dart b/lib/pages/sleep_report/component/WeekDataWidget.dart index 4078b8f..5ad86b9 100644 --- a/lib/pages/sleep_report/component/WeekDataWidget.dart +++ b/lib/pages/sleep_report/component/WeekDataWidget.dart @@ -15,6 +15,17 @@ Widget WeekDataWidget( Map sleepReport, dynamic data, ) { + List getDate() { + var s = buildWeekDatesAndPoints(sleepReport['scoreList']); + if (s == null || + s['dates'] == null || + s['dates'] is! List || + s['dates'].length != 7) { + return []; + } + return s['dates']; + } + List _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,62 +776,154 @@ int getWeekdayX(DateTime current, DateTime first) { return weekOffset * 7 + (current.weekday - 1); } -Map buildWeekDatesAndPoints(Map scoreList) { - if (!scoreList.containsKey('data') || (scoreList['data'] as List).isEmpty) { +// Map buildWeekDatesAndPoints(Map scoreList) { + +// if (scoreList == null || +// !scoreList.containsKey('data') || +// (scoreList['data'] as List).isEmpty) { +// return { +// 'dates': [], +// 'points': [], +// 'colors': [], +// }; +// } +// List> data = (scoreList['data'] as List) +// .where((item) => item is Map) +// .cast>() +// .toList(); + +// // 提取 level -> color 映射表 +// Map 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 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 points = []; +// List 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 buildWeekDatesAndPoints(Map? scoreList) { + try { + if (scoreList == null || + !scoreList.containsKey('data') || + (scoreList['data'] as List).isEmpty) { + return { + 'dates': [], + 'points': [], + 'colors': [], + }; + } + + List> data = (scoreList['data'] as List) + .where((item) => item is Map) + .cast>() + .toList(); + + if (data.isEmpty) { + return { + 'dates': [], + 'points': [], + 'colors': [], + }; + } + + // 提取 level -> color 映射表 + Map 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']; + } + } + } + + // 如果 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 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 points = []; + List 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'] is int) ? item['level'] : 0; + String color = levelColorMap[level] ?? "#FFFF00"; + + points.add(Offset(x, y)); + colors.add(color); + } + + return { + 'dates': dates, + 'points': points, + 'colors': colors, + }; + } catch (e, s) { + // ⚠️ 捕获异常并返回空结构 + debugPrint("❌ buildWeekDatesAndPoints 出错:$e"); + debugPrint("—— 异常堆栈 ——"); + debugPrintStack(stackTrace: s); + debugPrint("———————————"); return { 'dates': [], 'points': [], 'colors': [], }; } - - List> data = (scoreList['data'] as List) - .where((item) => item is Map) - .cast>() - .toList(); - - // 提取 level -> color 映射表 - Map 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 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 points = []; - List 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, - }; } List buildGeneralPoints(Map dyspData) { diff --git a/lib/pages/sleep_report/new_sleep_report_page.dart b/lib/pages/sleep_report/new_sleep_report_page.dart index 02179d4..442d90e 100644 --- a/lib/pages/sleep_report/new_sleep_report_page.dart +++ b/lib/pages/sleep_report/new_sleep_report_page.dart @@ -58,6 +58,7 @@ class _NewSleepReportPageState extends State { Future.microtask(() { if (Get.isRegistered()) { sleepReportController.isLoading.value = false; + sleepReportController.model.type = 1; sleepReportController.sleepReport.value = {}; } }); @@ -876,7 +877,7 @@ class _NewSleepReportPageState extends State { // } // }), Obx(() { - try { + if (sleepReportController.isLoading.value) { return Center( child: CircularProgressIndicator( @@ -908,12 +909,7 @@ class _NewSleepReportPageState extends State { default: return NullDataWidget(); } - } catch (e, s) { - debugPrint("❌ Obx 构建异常: $e"); - debugPrintStack( - stackTrace: s, label: "Obx build 详细堆栈:"); - return NullDataWidget(); - } + }), Column( children: [