blood_pressure.dart 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:get/get.dart';
  5. import 'package:vitalapp/pages/medical/widgets/device_status_position.dart';
  6. import 'package:vnote_device_plugin/consts/types.dart';
  7. import 'package:vnote_device_plugin/devices/nibp.dart';
  8. import 'package:vnote_device_plugin/models/exams/nibp.dart';
  9. import 'package:vitalapp/components/alert_dialog.dart';
  10. import 'package:vitalapp/managers/interfaces/models/device.dart';
  11. import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
  12. import 'package:vitalapp/pages/medical/controller.dart';
  13. import 'package:vitalapp/pages/medical/controllers/nibp.dart';
  14. import 'package:vitalapp/pages/medical/models/item.dart';
  15. import 'package:vitalapp/pages/medical/models/worker.dart';
  16. import 'package:vitalapp/pages/medical/widgets/device_status.dart';
  17. import 'package:vitalapp/pages/medical/widgets/switch_button.dart';
  18. /// TODO 需要优化
  19. /// 小弹窗输入
  20. class VDialogBloodPressure extends StatelessWidget {
  21. /// 标题
  22. final String? title;
  23. /// 描述
  24. final String? description;
  25. /// 输入占位符
  26. final String? placeholder;
  27. /// 初始值
  28. final List<String>? initialValue;
  29. const VDialogBloodPressure({
  30. super.key,
  31. this.title,
  32. this.description,
  33. this.placeholder,
  34. this.initialValue,
  35. });
  36. Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);
  37. @override
  38. Widget build(BuildContext context) {
  39. final controller1 = TextEditingController(text: initialValue?.first);
  40. final controller2 = TextEditingController(text: initialValue?.last);
  41. return VAlertDialog(
  42. title: title,
  43. width: 440,
  44. contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
  45. content: _buildContent(context, controller1, controller2),
  46. showCancel: true,
  47. onConfirm: () {
  48. Get.back(result: json.encode([controller1.text, controller2.text]));
  49. },
  50. );
  51. }
  52. Widget _buildContent(
  53. BuildContext context,
  54. TextEditingController controller1,
  55. TextEditingController controller2,
  56. ) {
  57. final children = <Widget>[];
  58. if (description != null) {
  59. children.add(
  60. Padding(
  61. padding: const EdgeInsets.symmetric(horizontal: 4),
  62. child: Text(
  63. description!,
  64. style: const TextStyle(color: Colors.black87, fontSize: 18),
  65. ),
  66. ),
  67. );
  68. children.add(const SizedBox(height: 8));
  69. } else {
  70. children.add(const SizedBox(height: 12));
  71. }
  72. children.add(_buildInputWidget(context, controller1, controller2));
  73. return SingleChildScrollView(
  74. child: Column(
  75. mainAxisSize: MainAxisSize.min,
  76. crossAxisAlignment: CrossAxisAlignment.start,
  77. children: children,
  78. ),
  79. );
  80. }
  81. Widget _buildInputWidget(
  82. BuildContext context,
  83. TextEditingController controller1,
  84. TextEditingController controller2,
  85. ) {
  86. const fontSize = 20.0;
  87. return Row(
  88. children: [
  89. _buildItem(context, controller1, '收缩压'),
  90. const Text(
  91. '/',
  92. style: TextStyle(fontSize: fontSize),
  93. ),
  94. _buildItem(context, controller2, '舒张压'),
  95. ],
  96. );
  97. }
  98. Widget _buildItem(
  99. BuildContext context,
  100. TextEditingController controller,
  101. String hintText,
  102. ) {
  103. const fontSize = 20.0;
  104. const height = 56.0;
  105. return Expanded(
  106. child: SizedBox(
  107. height: height,
  108. child: TextField(
  109. controller: controller,
  110. readOnly: false,
  111. autofocus: true,
  112. keyboardType: const TextInputType.numberWithOptions(
  113. decimal: true), // 允许输入数字和小数点
  114. inputFormatters: [
  115. FilteringTextInputFormatter.allow(
  116. RegExp(r'^\d+\.?\d{0,2}'),
  117. ), // 只允许输入数字和小数点,俩位小数
  118. ],
  119. style: const TextStyle(fontSize: fontSize),
  120. decoration: InputDecoration(
  121. border: const UnderlineInputBorder(
  122. borderRadius: BorderRadius.zero,
  123. borderSide: BorderSide(),
  124. ),
  125. enabledBorder: const UnderlineInputBorder(
  126. borderRadius: BorderRadius.zero,
  127. borderSide: BorderSide(
  128. color: Colors.black54,
  129. ),
  130. ),
  131. focusedBorder: UnderlineInputBorder(
  132. borderRadius: BorderRadius.zero,
  133. borderSide: BorderSide(
  134. color: Theme.of(context).primaryColor.withOpacity(.4),
  135. ),
  136. ),
  137. filled: true,
  138. fillColor: Colors.white,
  139. contentPadding: const EdgeInsets.symmetric(
  140. vertical: (height - fontSize * 1.2) / 2,
  141. horizontal: 8,
  142. ),
  143. hintStyle: const TextStyle(fontSize: fontSize),
  144. labelStyle: const TextStyle(fontSize: fontSize),
  145. hintText: hintText,
  146. isCollapsed: true,
  147. ),
  148. onSubmitted: (value) {
  149. print(value);
  150. Get.back(result: value);
  151. },
  152. ),
  153. ),
  154. );
  155. }
  156. }
  157. // ignore: must_be_immutable
  158. class BloodPressure extends StatefulWidget {
  159. const BloodPressure({
  160. super.key,
  161. // required this.currentValue,
  162. // required this.bloodPressure,
  163. });
  164. // Map<String, dynamic> currentValue;
  165. // Function(Map<String, dynamic>) bloodPressure;
  166. @override
  167. State<BloodPressure> createState() => _ExamBloodPressureState();
  168. }
  169. class _ExamBloodPressureState extends State<BloodPressure> {
  170. var controller = Get.find<MedicalController>();
  171. PressureStatus pressureStatus = PressureStatus.left;
  172. late NibpDeviceController nibp;
  173. NibpDeviceWorker? worker;
  174. int liveValue = 0;
  175. int errorCount = 0; //设备重连失败次数
  176. late NibpExamValue? value = NibpExamValue(
  177. diastolicPressure: controller.diagnosisDataValue['NIBP']?['Sbp'],
  178. // int.parse(controller.diagnosisDataValue['NIBP']?['Sbp'] ?? '0'),
  179. systolicPressure: controller.diagnosisDataValue['NIBP']?['Dbp'],
  180. // int.parse(controller.diagnosisDataValue['NIBP']?['Dbp'] ?? '0'),
  181. pulse: 0);
  182. WorkerStatus _connectStatus = WorkerStatus.connecting;
  183. @override
  184. void initState() {
  185. currentDevice();
  186. super.initState();
  187. }
  188. Future<void> currentDevice() async {
  189. DeviceModel? device = await controller.getDevice(DeviceTypes.NIBP);
  190. if (device.isNull) {
  191. _connectStatus = WorkerStatus.unboundDevice;
  192. worker = null;
  193. setState(() {});
  194. return;
  195. }
  196. nibp = NibpDeviceController(device?.model ?? '', device?.mac ?? '');
  197. worker = nibp.worker;
  198. _connectStatus = nibp.connectStatus;
  199. loadListeners();
  200. }
  201. void loadListeners() {
  202. worker!.liveUpdateEvent.addListener(_onLiveUpdate);
  203. worker!.resultUpdateEvent.addListener(_onSuccess);
  204. worker!.connectErrorEvent.addListener(_onConnectFail);
  205. worker!.connectedEvent.addListener(_onConnectSuccess);
  206. worker!.connect();
  207. }
  208. Future<void> disconnect() async {
  209. try {
  210. if (worker != null) {
  211. await worker!.disconnect();
  212. worker!.connectErrorEvent.removeListener(_onConnectFail);
  213. worker!.connectedEvent.removeListener(_onConnectSuccess);
  214. worker!.liveUpdateEvent.removeListener(_onLiveUpdate);
  215. worker!.resultUpdateEvent.removeListener(_onSuccess);
  216. worker!.disconnectedEvent.removeListener(_onDisconnected);
  217. }
  218. } catch (err) {
  219. print(err);
  220. }
  221. }
  222. @override
  223. void dispose() {
  224. disconnect();
  225. worker?.dispose();
  226. super.dispose();
  227. }
  228. void _onLiveUpdate(_, int e) {
  229. setState(() {
  230. liveValue = e;
  231. });
  232. }
  233. /// TODO 需求不清,检测的数据需要传给体检,但是检测又不区分左右侧血压
  234. void _onSuccess(_, NibpExamValue e) {
  235. setState(() {
  236. value = e;
  237. /// 这是第三方需要的数据
  238. controller.diagnosisDataValue['NIBP'] = {
  239. 'Sbp': value?.diastolicPressure ?? '0',
  240. 'Dbp': value?.systolicPressure ?? '0',
  241. 'Pulse_Beat': value?.pulse,
  242. };
  243. controller.saveCachedRecord();
  244. // /// 这是我们需要的数据
  245. // if (pressureStatus == PressureStatus.left) {
  246. // controller.diagnosisDataValue['NIBP']['Sbp_Left'] =
  247. // value?.diastolicPressure ?? '0';
  248. // controller.diagnosisDataValue['NIBP']['Dbp_Left'] =
  249. // value?.diastolicPressure ?? '0';
  250. // } else {
  251. // controller.diagnosisDataValue['NIBP']['Sbp_Right'] =
  252. // value?.diastolicPressure ?? '0';
  253. // controller.diagnosisDataValue['NIBP']['Dbp_Right'] =
  254. // value?.diastolicPressure ?? '0';
  255. // }
  256. });
  257. }
  258. void _onConnectFail(sender, e) {
  259. print('连接设备失败');
  260. if (errorCount < 3) {
  261. worker?.connect();
  262. }
  263. setState(() {
  264. errorCount++;
  265. _connectStatus = WorkerStatus.connectionFailed;
  266. });
  267. }
  268. void _onDisconnected(sender, e) {
  269. print('设备连接中断');
  270. if (errorCount < 3) {
  271. worker?.connect();
  272. }
  273. setState(() {
  274. errorCount++;
  275. _connectStatus = WorkerStatus.disconnected;
  276. });
  277. }
  278. void _onConnectSuccess(sender, e) {
  279. _connectStatus = WorkerStatus.connected;
  280. setState(() {});
  281. }
  282. Widget _buildValue() {
  283. if (controller.diagnosisDataValue['NIBP']?['Sbp'] != null) {
  284. return _buildResultWidget();
  285. }
  286. return _buildLiveWidget();
  287. }
  288. @override
  289. Widget build(BuildContext context) {
  290. return Stack(
  291. children: [
  292. ExamCard(
  293. clickCard: () async {
  294. String? result = await VDialogBloodPressure(
  295. title: pressureStatus == PressureStatus.left ? '左侧血压' : '右侧血压',
  296. initialValue: [
  297. controller.diagnosisDataValue['NIBP']?['Sbp'] ?? '',
  298. controller.diagnosisDataValue['NIBP']?['Dbp'] ?? '',
  299. ],
  300. ).show();
  301. if (result != null) {
  302. controller.diagnosisDataValue['NIBP'] = {
  303. 'Sbp': jsonDecode(result)?.first,
  304. 'Dbp': jsonDecode(result)?.last,
  305. 'Pulse_Beat': '70',
  306. };
  307. print(result);
  308. value = NibpExamValue(
  309. diastolicPressure: int.parse(jsonDecode(result)?.first),
  310. pulse: 70,
  311. systolicPressure: int.parse(jsonDecode(result)?.last),
  312. );
  313. controller.saveCachedRecord();
  314. print(value);
  315. // value.diastolicPressure = jsonDecode(result)?.first;
  316. // systolicPressure: jsonDecode(result)?.last ?? 0,
  317. // print(value?.diastolicPressure);
  318. }
  319. setState(() {});
  320. },
  321. title: '血压',
  322. content: Container(
  323. padding: const EdgeInsets.only(top: 50),
  324. child: _SideBar(
  325. value: _buildValue(),
  326. unit: 'mmHg',
  327. ),
  328. ),
  329. ),
  330. if (errorCount < 3)
  331. DeviceStatusPosition(
  332. deviceStatus: DeviceStatus(connectStatus: _connectStatus),
  333. ),
  334. if (errorCount >= 3) _buildErrorButton(),
  335. ],
  336. );
  337. }
  338. /// 需要封装一下
  339. Widget _buildErrorButton() {
  340. return DeviceStatusPosition(
  341. deviceStatus: Row(
  342. children: [
  343. const Text(
  344. '请确认设备是否启动',
  345. style: TextStyle(fontSize: 24, color: Colors.red),
  346. ),
  347. const SizedBox(
  348. width: 8,
  349. ),
  350. IconButton(
  351. onPressed: () {
  352. worker?.connect();
  353. setState(() {
  354. _connectStatus = WorkerStatus.connecting;
  355. errorCount = 0;
  356. });
  357. },
  358. icon: const Icon(Icons.refresh),
  359. iconSize: 32,
  360. ),
  361. const SizedBox(
  362. width: 32,
  363. ),
  364. ],
  365. ),
  366. );
  367. }
  368. Widget _buildLeftOrRightBloodPressure() {
  369. return Positioned(
  370. top: 130,
  371. left: 38,
  372. child: AnimatedToggle(
  373. values: const ['左侧', '右侧'],
  374. onToggleCallback: (value) {
  375. pressureStatus = value;
  376. setState(() {});
  377. },
  378. statusValue: pressureStatus,
  379. buttonColor: Theme.of(context).primaryColor,
  380. backgroundColor: const Color(0xFFB5C1CC),
  381. textColor: const Color(0xFFFFFFFF),
  382. ),
  383. // child: Text('Toggle Value : $_toggleValue'),
  384. );
  385. }
  386. Widget _buildLiveWidget() {
  387. return Center(
  388. child: RichText(
  389. text: TextSpan(
  390. text: liveValue.toString() == '0' ? '--' : liveValue.toString(),
  391. style: const TextStyle(
  392. fontSize: 80,
  393. color: Colors.black,
  394. ),
  395. children: const [
  396. TextSpan(text: ' '),
  397. TextSpan(
  398. text: 'mmHg',
  399. style: TextStyle(fontSize: 25),
  400. )
  401. ],
  402. ),
  403. ),
  404. );
  405. }
  406. Widget _buildResultWidget() {
  407. const textStyle = TextStyle(
  408. fontSize: 48,
  409. );
  410. return Stack(
  411. children: [
  412. Column(
  413. mainAxisAlignment: MainAxisAlignment.center,
  414. mainAxisSize: MainAxisSize.min,
  415. crossAxisAlignment: CrossAxisAlignment.center,
  416. children: [
  417. Align(
  418. alignment: Alignment.centerLeft,
  419. child: Text(
  420. value!.systolicPressure.toString(),
  421. style: textStyle,
  422. ),
  423. ),
  424. Align(
  425. alignment: Alignment.centerRight,
  426. child: Text(
  427. value!.diastolicPressure.toString(),
  428. style: textStyle,
  429. ),
  430. ),
  431. ],
  432. ),
  433. const Positioned.fill(
  434. child: Center(
  435. child: Text(
  436. " /",
  437. style: TextStyle(fontSize: 24),
  438. ),
  439. ),
  440. ),
  441. ],
  442. );
  443. }
  444. }
  445. class _SideBar extends StatelessWidget {
  446. final Widget value;
  447. final String unit;
  448. const _SideBar({
  449. required this.value,
  450. required this.unit,
  451. });
  452. @override
  453. Widget build(BuildContext context) {
  454. return Row(
  455. mainAxisAlignment: MainAxisAlignment.end,
  456. crossAxisAlignment: CrossAxisAlignment.start,
  457. children: [
  458. Container(
  459. alignment: Alignment.bottomRight,
  460. padding: const EdgeInsets.only(
  461. bottom: 20,
  462. right: 30,
  463. left: 40,
  464. ),
  465. child: FittedBox(
  466. child: Row(
  467. mainAxisAlignment: MainAxisAlignment.end,
  468. crossAxisAlignment: CrossAxisAlignment.end,
  469. children: [
  470. value,
  471. ],
  472. ),
  473. ),
  474. ),
  475. ],
  476. );
  477. }
  478. }