import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:vitalapp/components/dialog_ecg_image.dart'; import 'package:vitalapp/pages/medical/widgets/twelve_ecg_view/widgets/conclusion_dialog.dart'; import 'package:vnote_device_plugin/models/exams/twelve_heart.dart'; class LastRecordTable extends StatelessWidget { final bool? isExistLocalData; const LastRecordTable({ super.key, this.title, required this.columnNames, this.columnWidths, required this.tableData, this.diagnosisTime, this.isExistLocalData = false, this.checkKey, }); final List> tableData; final List columnNames; final String? title; final String? diagnosisTime; final String? checkKey; final Map? columnWidths; @override Widget build(BuildContext context) { return Container(child: Expanded(child: _buildTable())); } Widget _buildTable() { final scrollController = ScrollController(); return Column( children: [ const SizedBox( height: 10, ), Table( columnWidths: columnWidths, children: [ TableRow( decoration: const BoxDecoration( color: Color.fromRGBO(215, 234, 255, 1), ), children: columnNames .map((columnName) => _buildHeaderCell(columnName)) .toList(), ), ], ), Expanded( child: Scrollbar( controller: scrollController, thumbVisibility: true, child: SingleChildScrollView( controller: scrollController, child: Table( columnWidths: columnWidths, children: _buildRow(tableData), ), ), ), ), const SizedBox( height: 10, ), ], ); } List _buildRow(List> tableData) { // var controller = Get.find(); var tableRows = []; for (var i = 0; i < tableData.length; i++) { final List rowData = tableData[i]; final label = rowData[0]; List children = []; for (String child in rowData) { if (rowData.indexOf(child) == 3) { continue; } if (checkKey == "HEITCMC") { children.add(_buildConstitutionDataCell(child)); } else if (label == '心电测量') { children.add(_buildImageDataCell(child)); } else if (label == '十二导心电图(30秒)' || label == "十二导心电图(30秒)") { children.add(_buildImageDataCell(child)); } else if (label == '十二导心电图(5秒)' || label == '十二导心电图(5秒)' || label == '十二导心电图') { children.add(_buildImageDataCell(child)); } else if (label == '十二导分析结果') { children.add(_buildEcg12DataCell(child)); } else { children.add(_buildDataCell(child)); } } tableRows.add( TableRow( decoration: i % 2 == 0 ? null : const BoxDecoration( color: Color.fromRGBO(233, 244, 255, 1), ), children: children, ), ); } return tableRows; } TableCell _buildConstitutionDataCell(String title) { if (title.contains("Value")) { title = jsonDecode(title)["Value"].toString(); } else if (title.contains("质")) { title = title.substring(0, 3); } Widget contentWidget = _buildSizeOverflow(title); return TableCell( child: Container( padding: const EdgeInsets.symmetric(horizontal: 14), alignment: Alignment.centerLeft, height: 37, child: contentWidget, ), ); } TableCell _buildHeaderCell(String title) { return TableCell( child: Container( padding: const EdgeInsets.symmetric(horizontal: 14), alignment: Alignment.centerLeft, height: 37, child: Text( title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), ), ); } /// 12导心电的结果 TableCell _buildEcg12DataCell(String value) { if (value.contains("advice")) { return TableCell( child: InkWell( onTap: () async { TwelveHeartResultEntity resultConclusion = TwelveHeartResultEntity.fromJson(jsonDecode(value)); await ConclusionDialog.show(twelveHeartResult: resultConclusion); }, child: Container( padding: const EdgeInsets.symmetric(horizontal: 14), alignment: Alignment.centerLeft, height: 57, child: const Text( "点击查看结果", style: TextStyle( color: Colors.blue, ), ), ), ), ); } return TableCell( child: Container( padding: const EdgeInsets.symmetric(horizontal: 14), alignment: Alignment.centerLeft, height: 57, child: Text( value, style: const TextStyle(fontSize: 14), ), ), ); } String _replaceSemicolon(String input) { String result = input; if (result.endsWith(';')) { result = result.replaceAll(';', ''); } else { result = result.replaceAll(';', '、'); } return result; } Widget _buildSizeOverflow(String title) { return LayoutBuilder( builder: (BuildContext context, BoxConstraints constraints) { final textPainter = TextPainter( text: TextSpan(text: title, style: const TextStyle(fontSize: 14)), maxLines: 2, textDirection: TextDirection.ltr, )..layout(maxWidth: constraints.maxWidth); Widget contentWidget = Text( title, style: const TextStyle( color: Colors.black, fontSize: 14, ), maxLines: 2, overflow: TextOverflow.ellipsis, ); if (textPainter.didExceedMaxLines) { contentWidget = Tooltip( message: title, padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12), textStyle: const TextStyle(fontSize: 14, color: Colors.white), triggerMode: TooltipTriggerMode.tap, child: contentWidget, ); } else { // 文本没有超出 print('Text did not overflow'); } return contentWidget; }, ); } TableCell _buildDataCell(String title) { if (title.contains(";")) { title = _replaceSemicolon(title); } Widget contentWidget = _buildSizeOverflow(title); return TableCell( child: Container( padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 2), alignment: Alignment.centerLeft, constraints: BoxConstraints(maxHeight: 48), child: Row(children: [Expanded(child: contentWidget)]), ), ); } TableCell _buildImageDataCell(String title) { if (title.length > 50) { Uint8List imageBytes = (isExistLocalData! && !kIsWeb) ? base64.decode(title) : base64.decode( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII="); return TableCell( child: InkWell( onTap: () { print(title); Get.dialog( EcgImageDialog( image: (isExistLocalData! && !kIsWeb) ? Image.memory( imageBytes, ) : Image.network( title, fit: BoxFit.cover, ), ), ); }, child: Container( padding: const EdgeInsets.symmetric(horizontal: 8), alignment: Alignment.center, height: 40, child: isExistLocalData! ? Image.memory( imageBytes, fit: BoxFit.fitHeight, height: 28, ) : Image.network( title, fit: BoxFit.fitHeight, height: 28, ), ), ), ); } return TableCell( child: Container( padding: const EdgeInsets.symmetric(horizontal: 14), alignment: Alignment.centerLeft, height: 37, child: Text( title, style: const TextStyle(fontSize: 14), ), ), ); } }