blood_pressure.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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:vnote_device_plugin/devices/nibp.dart';
  6. import 'package:vnote_device_plugin/models/exams/nibp.dart';
  7. import 'package:vnoteapp/components/alert_dialog.dart';
  8. import 'package:vnoteapp/pages/check/widgets/exam_configurable/exam_card.dart';
  9. import 'package:vnoteapp/managers/interfaces/permission.dart';
  10. import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect.dart';
  11. import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect_disconnected.dart';
  12. import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect_fail.dart';
  13. import 'package:vnoteapp/pages/check/widgets/exam_device_connect_status/connect_success.dart';
  14. /// 小弹窗输入
  15. class VDialogBloodPressure extends StatelessWidget {
  16. /// 标题
  17. final String? title;
  18. /// 描述
  19. final String? description;
  20. /// 输入占位符
  21. final String? placeholder;
  22. /// 初始值
  23. final List<String>? initialValue;
  24. const VDialogBloodPressure({
  25. super.key,
  26. this.title,
  27. this.description,
  28. this.placeholder,
  29. this.initialValue,
  30. });
  31. Future<String?> show<String>() => VAlertDialog.showDialog<String>(this);
  32. @override
  33. Widget build(BuildContext context) {
  34. final controller1 = TextEditingController(text: initialValue?.first);
  35. final controller2 = TextEditingController(text: initialValue?.last);
  36. return VAlertDialog(
  37. title: title,
  38. width: 440,
  39. contentPadding: const EdgeInsets.symmetric(vertical: 12, horizontal: 24),
  40. content: _buildContent(context, controller1, controller2),
  41. showCancel: true,
  42. onConfirm: () {
  43. Get.back(result: json.encode([controller1.text, controller2.text]));
  44. },
  45. );
  46. }
  47. Widget _buildContent(
  48. BuildContext context,
  49. TextEditingController controller1,
  50. TextEditingController controller2,
  51. ) {
  52. final children = <Widget>[];
  53. if (description != null) {
  54. children.add(
  55. Padding(
  56. padding: const EdgeInsets.symmetric(horizontal: 4),
  57. child: Text(
  58. description!,
  59. style: const TextStyle(color: Colors.black87, fontSize: 18),
  60. ),
  61. ),
  62. );
  63. children.add(const SizedBox(height: 8));
  64. } else {
  65. children.add(const SizedBox(height: 12));
  66. }
  67. children.add(_buildInputWidget(context, controller1, controller2));
  68. return SingleChildScrollView(
  69. child: Column(
  70. mainAxisSize: MainAxisSize.min,
  71. crossAxisAlignment: CrossAxisAlignment.start,
  72. children: children,
  73. ),
  74. );
  75. }
  76. Widget _buildInputWidget(
  77. BuildContext context,
  78. TextEditingController controller1,
  79. TextEditingController controller2,
  80. ) {
  81. const fontSize = 20.0;
  82. return Row(
  83. children: [
  84. _buildItem(context, controller1, '高压'),
  85. const Text(
  86. '/',
  87. style: TextStyle(fontSize: fontSize),
  88. ),
  89. _buildItem(context, controller2, '低压'),
  90. ],
  91. );
  92. }
  93. Widget _buildItem(
  94. BuildContext context,
  95. TextEditingController controller,
  96. String hintText,
  97. ) {
  98. const fontSize = 20.0;
  99. const height = 56.0;
  100. return Expanded(
  101. child: SizedBox(
  102. height: height,
  103. child: TextField(
  104. controller: controller,
  105. readOnly: false,
  106. autofocus: true,
  107. keyboardType: const TextInputType.numberWithOptions(
  108. decimal: true), // 允许输入数字和小数点
  109. inputFormatters: [
  110. FilteringTextInputFormatter.allow(
  111. RegExp(r'^\d+\.?\d{0,2}'),
  112. ), // 只允许输入数字和小数点,俩位小数
  113. ],
  114. style: const TextStyle(fontSize: fontSize),
  115. decoration: InputDecoration(
  116. border: const UnderlineInputBorder(
  117. borderRadius: BorderRadius.zero,
  118. borderSide: BorderSide(),
  119. ),
  120. enabledBorder: const UnderlineInputBorder(
  121. borderRadius: BorderRadius.zero,
  122. borderSide: BorderSide(
  123. color: Colors.black54,
  124. ),
  125. ),
  126. focusedBorder: UnderlineInputBorder(
  127. borderRadius: BorderRadius.zero,
  128. borderSide: BorderSide(
  129. color: Theme.of(context).primaryColor.withOpacity(.4),
  130. ),
  131. ),
  132. filled: true,
  133. fillColor: Colors.white,
  134. contentPadding: const EdgeInsets.symmetric(
  135. vertical: (height - fontSize * 1.2) / 2,
  136. horizontal: 8,
  137. ),
  138. hintStyle: const TextStyle(fontSize: fontSize),
  139. labelStyle: const TextStyle(fontSize: fontSize),
  140. hintText: hintText,
  141. isCollapsed: true,
  142. ),
  143. onSubmitted: (value) {
  144. print(value);
  145. Get.back(result: value);
  146. },
  147. ),
  148. ),
  149. );
  150. }
  151. }
  152. // ignore: must_be_immutable
  153. class BloodPressure extends StatefulWidget {
  154. const BloodPressure({
  155. super.key,
  156. // required this.currentValue,
  157. // required this.bloodPressure,
  158. });
  159. // Map<String, dynamic> currentValue;
  160. // Function(Map<String, dynamic>) bloodPressure;
  161. @override
  162. State<BloodPressure> createState() => _ExamBloodPressureState();
  163. }
  164. class _ExamBloodPressureState extends State<BloodPressure> {
  165. var permissionManager = Get.find<IPermissionManager>();
  166. late final NibpDeviceWorker worker = NibpDeviceWorker(
  167. mac: 'A4:C1:38:33:84:F8',
  168. model: 'AOJ-30B',
  169. );
  170. late int liveValue = 0;
  171. NibpExamValue? value;
  172. // late String spO2 = widget.currentValue['Spo2'];
  173. bool _connectFailStatus = false;
  174. bool _connectSuccessStatus = false;
  175. bool _isConnect = false;
  176. /// 设备连接中断的状态
  177. bool _connectDisconnectedStatus = false;
  178. @override
  179. void initState() {
  180. getPermission();
  181. worker.liveUpdateEvent.addListener(_onLiveUpdate);
  182. worker.resultUpdateEvent.addListener(_onSuccess);
  183. worker.connectErrorEvent.addListener(_onConnectFail);
  184. worker.connectedEvent.addListener(_onConnectSuccess);
  185. connect();
  186. super.initState();
  187. }
  188. Future<void> connect() async {
  189. _connectFailStatus = false;
  190. _isConnect = true;
  191. _connectSuccessStatus = false;
  192. setState(() {});
  193. await worker.connect();
  194. }
  195. Future<void> disconnect() async {
  196. worker.connectErrorEvent.removeListener(_onConnectFail);
  197. worker.connectedEvent.removeListener(_onConnectSuccess);
  198. worker.liveUpdateEvent.removeListener(_onLiveUpdate);
  199. worker.resultUpdateEvent.removeListener(_onSuccess);
  200. worker.disconnectedEvent.removeListener(_onDisconnected);
  201. await worker.disconnect();
  202. }
  203. @override
  204. void dispose() {
  205. disconnect();
  206. super.dispose();
  207. }
  208. void _onLiveUpdate(_, int e) {
  209. setState(() {
  210. liveValue = e;
  211. });
  212. }
  213. void _onSuccess(_, NibpExamValue e) {
  214. setState(() {
  215. value = e;
  216. });
  217. }
  218. void _onConnectFail(sender, e) {
  219. print('连接设备失败');
  220. _connectFailStatus = true;
  221. _connectSuccessStatus = false;
  222. _isConnect = false;
  223. _connectDisconnectedStatus = false;
  224. setState(() {});
  225. }
  226. void _onConnectSuccess(sender, e) {
  227. _connectSuccessStatus = true;
  228. _connectFailStatus = false;
  229. _isConnect = false;
  230. _connectDisconnectedStatus = false;
  231. setState(() {});
  232. }
  233. void _onDisconnected(sender, e) {
  234. print('设备连接中断');
  235. _connectDisconnectedStatus = true;
  236. _connectSuccessStatus = false;
  237. _connectFailStatus = false;
  238. _isConnect = false;
  239. setState(() {});
  240. }
  241. Future<void> getPermission() async {
  242. await permissionManager.requestLocationPermission();
  243. await permissionManager.requestBluetoothConnectPermission();
  244. await permissionManager.requestBluetoothAdvertisePermission();
  245. await permissionManager.requestBluetoothScanPermission();
  246. }
  247. Widget _buildValue() {
  248. if (value != null) {
  249. return _buildResultWidget();
  250. }
  251. return _buildLiveWidget();
  252. }
  253. @override
  254. Widget build(BuildContext context) {
  255. return Stack(
  256. children: [
  257. ExamCard(
  258. title: '血压',
  259. // clickCard: () {},
  260. content: Container(
  261. padding: const EdgeInsets.only(top: 50),
  262. child: _SideBar(
  263. value: _buildValue(),
  264. unit: 'mmHg',
  265. ),
  266. ),
  267. ),
  268. if (_connectFailStatus)
  269. DeviceConnectFail(
  270. connect: () => connect(),
  271. ),
  272. if (_connectSuccessStatus) const DeviceConnectSuccess(),
  273. if (_isConnect) const DeviceConnect(),
  274. if (_connectDisconnectedStatus)
  275. DeviceConnectDisconnected(
  276. connect: () => connect(),
  277. ),
  278. ],
  279. );
  280. }
  281. Widget _buildLiveWidget() {
  282. final textStyle = TextStyle(fontSize: 80, color: Colors.orange.shade700);
  283. return Center(
  284. child: Text(
  285. liveValue.toString(),
  286. style: textStyle,
  287. ),
  288. );
  289. }
  290. Widget _buildResultWidget() {
  291. const textStyle = TextStyle(fontSize: 48, color: Colors.green);
  292. return Stack(
  293. children: [
  294. Column(
  295. mainAxisAlignment: MainAxisAlignment.center,
  296. mainAxisSize: MainAxisSize.min,
  297. crossAxisAlignment: CrossAxisAlignment.center,
  298. children: [
  299. Align(
  300. alignment: Alignment.centerLeft,
  301. child: Text(
  302. value!.systolicPressure.toString(),
  303. style: textStyle,
  304. ),
  305. ),
  306. Align(
  307. alignment: Alignment.centerRight,
  308. child: Text(
  309. value!.diastolicPressure.toString(),
  310. style: textStyle,
  311. ),
  312. ),
  313. ],
  314. ),
  315. const Positioned.fill(
  316. child: Center(
  317. child: Text(
  318. " /",
  319. style: TextStyle(fontSize: 24),
  320. ),
  321. ),
  322. ),
  323. ],
  324. );
  325. }
  326. }
  327. class _SideBar extends StatelessWidget {
  328. final Widget value;
  329. final String unit;
  330. const _SideBar({
  331. required this.value,
  332. required this.unit,
  333. });
  334. @override
  335. Widget build(BuildContext context) {
  336. return Row(
  337. mainAxisAlignment: MainAxisAlignment.end,
  338. crossAxisAlignment: CrossAxisAlignment.start,
  339. children: [
  340. Container(
  341. alignment: Alignment.bottomRight,
  342. padding: const EdgeInsets.only(
  343. bottom: 20,
  344. right: 30,
  345. left: 40,
  346. ),
  347. child: FittedBox(
  348. child: Row(
  349. mainAxisAlignment: MainAxisAlignment.end,
  350. crossAxisAlignment: CrossAxisAlignment.end,
  351. children: [
  352. value,
  353. // RichText(
  354. // text: TextSpan(
  355. // text: value,
  356. // style: const TextStyle(
  357. // fontSize: 80,
  358. // color: Colors.black,
  359. // ),
  360. // children: [
  361. // TextSpan(
  362. // text: unit,
  363. // style: const TextStyle(fontSize: 25),
  364. // )
  365. // ],
  366. // ),
  367. // ),
  368. ],
  369. ),
  370. ),
  371. ),
  372. ],
  373. );
  374. }
  375. }