import 'package:ef/ef.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:vbvs_app/common/color/appConstants.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'; import 'package:vbvs_app/enum/APPPackageType.dart'; import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart'; import 'package:vbvs_app/pages/sleep_report/chart/LineChartByRange.dart'; import 'package:EasyDartModule/EasyDartModule.dart' as es; class BreathePauseNewWidget extends StatefulWidget { var sleepReport; BreathePauseNewWidget({super.key, required this.sleepReport}); @override State createState() => _SnoreViewWidgetWidgetState(); } class _SnoreViewWidgetWidgetState extends State { @override void setState(VoidCallback callback) { super.setState(callback); } @override void initState() { super.initState(); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { try { if (widget.sleepReport == null || widget.sleepReport is! Map || widget.sleepReport.isEmpty) { return Container(); } List standard = widget.sleepReport['brs'] ?? []; final Map? result = standard.cast().firstWhere( (element) => element['id'] == 302, orElse: () => {}, ); int threshold = 0; if (result != null && result.isNotEmpty) { final rangeValue = result['range']; if (rangeValue is int) { threshold = rangeValue; } else if (rangeValue is String) { threshold = int.tryParse(rangeValue) ?? 0; } } List> data = (widget.sleepReport['asp'] as List).cast>(); // data = [ // {"st": 1763494195669, "value": 11}, // {"st": 1763494278485, "value": 18}, // {"st": 1763494293453, "value": 18}, // {"st": 1763494352321, "value": 14}, // {"st": 1763494606757, "value": 12} // ]; List> showLabel = convertToShowLabel(data); double maxTimes = 70; try { maxTimes = showLabel.fold(0.0, (prev, element) { double currentTimes = (element['times'] ?? 0).toDouble(); // 转换为 double 类型 return currentTimes > prev ? currentTimes : prev; }); } catch (e) { print("$e"); } // 向十位数取整 int roundedMaxTimes = (maxTimes / 10).ceil() * 10; var startTime = widget.sleepReport['startTime']; var endTime = widget.sleepReport['endTime']; if (showLabel.isEmpty) return const SizedBox(); return Container( width: double.infinity, decoration: BoxDecoration( color: themeController.currentColor.sc5, borderRadius: BorderRadius.circular( AppConstants().normal_container_radius), // 你可以按需调整圆角半径 ), child: Padding( padding: EdgeInsetsDirectional.fromSTEB(26.rpx, 29.rpx, 26.rpx, 0.rpx), child: Column( mainAxisSize: MainAxisSize.max, children: [ Container( child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Text( "呼吸暂停监测".tr, style: TextStyle( color: themeController.currentColor.sc3, fontSize: AppConstants().title_text_fontSize), ), ClickableContainer( backgroundColor: Colors.transparent, highlightColor: Colors.white, // 或设置为你需要的水波纹颜色 padding: EdgeInsetsDirectional.fromSTEB( 14.rpx, 10.rpx, 14.rpx, 10.rpx), // borderRadius: 0.rpx, // 圆形点击区域 onTap: () { if (AppConstants().ent_type == APPPackageType.MHT.code) { showTipDialog( context, Container( child: Text( // "呼吸暂停监测介绍。", "呼吸暂停监测是指用户在睡眠过程中产生的呼吸暂停的图表说明。".tr, style: TextStyle( fontSize: 26.rpx, color: Colors.black, ), ), ), backgroundColor: Color(0xFFFFFFFF), colors: [ Color(0XFF1592AA), Color(0xFF0C83A7), Color(0xFF006FA3) ], ); } else { showTipDialog( context, Container( child: Text( "呼吸暂停监测是指用户在睡眠过程中产生的呼吸暂停的图表说明。".tr, style: TextStyle( fontSize: 26.rpx, color: themeController.currentColor.sc3, ), ), ), backgroundColor: themeController.currentColor.sc17, colors: AppConstants().thNormalButton, ); } }, child: Container( padding: EdgeInsetsDirectional.fromSTEB( 0, 0.rpx, 0.rpx, 0), // 外部 padding 移到内部 width: 28.rpx, height: 28.rpx, child: SvgPicture.asset( 'assets/img/icon/explain.svg', fit: BoxFit.cover, color: themeController.currentColor.sc4, ), ), ), ], ), ), SizedBox( height: 32.rpx, ), Row( children: [ Text( "秒".tr, style: TextStyle( color: stringToColor("#FFFFFF"), fontSize: 18.rpx), ), ], ), Padding( padding: EdgeInsetsDirectional.fromSTEB(0.rpx, 40.rpx, 0.rpx, 0.rpx), child: LineChartByRange( showLabel: showLabel, startTime: startTime, endTime: endTime, threshold: threshold != 0 ? threshold : null, maxY: roundedMaxTimes, ySegments: 7, ), ), SizedBox( height: 52.rpx, ), ], ), ), ); } catch (e) { es.EasyDartModule.logger.error("打鼾监测绘制异常${e}"); return Container(); } } List> convertToShowLabel( List> data) { if (data.isEmpty) return []; data.sort((a, b) => a['st'].compareTo(b['st'])); // 确保时间有序 List> result = []; int startTime = data[0]['st']; int endTime = data[0]['st']; int currentValue = data[0]['value']; for (int i = 1; i < data.length; i++) { final item = data[i]; final st = item['st']; final value = item['value']; if (value == currentValue) { // endTime = st; result.add({ "startTime": startTime, "endTime": endTime, "times": currentValue, }); startTime = st; endTime = st; currentValue = value; } else { result.add({ "startTime": startTime, "endTime": endTime, "times": currentValue, }); startTime = st; endTime = st; currentValue = value; } } // 添加最后一段 result.add({ "startTime": startTime, "endTime": endTime, "times": currentValue, }); return result; } }