body_bmi.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vitalapp/pages/medical/widgets/device_status_position.dart';
  4. import 'package:vnote_device_plugin/consts/types.dart';
  5. import 'package:vnote_device_plugin/devices/weight.dart';
  6. import 'package:vitalapp/managers/interfaces/models/device.dart';
  7. import 'package:vitalapp/pages/check/widgets/exam_configurable/exam_card.dart';
  8. import 'package:vitalapp/components/dialog_number.dart';
  9. import 'package:vitalapp/pages/medical/controller.dart';
  10. import 'package:vitalapp/pages/medical/controllers/bmi.dart';
  11. import 'package:vitalapp/pages/medical/models/worker.dart';
  12. import 'package:vitalapp/pages/medical/widgets/device_status.dart';
  13. import 'package:vitalapp/pages/medical/widgets/side_bar.dart';
  14. class BodyWeight extends StatefulWidget {
  15. const BodyWeight({
  16. super.key,
  17. });
  18. @override
  19. State<BodyWeight> createState() => _ExamBodyWeightState();
  20. }
  21. class _ExamBodyWeightState extends State<BodyWeight> {
  22. var controller = Get.find<MedicalController>();
  23. late BmiDeviceController bmi;
  24. WeightDeviceWorker? worker;
  25. int errorCount = 0; //设备重连失败次数
  26. WorkerStatus _connectStatus = WorkerStatus.connecting;
  27. late String _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
  28. late String _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
  29. late String _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
  30. @override
  31. void initState() {
  32. initBmi();
  33. super.initState();
  34. }
  35. Future<void> connect() async {
  36. await worker!.connect();
  37. }
  38. Future<void> disconnect() async {
  39. if (worker != null) {
  40. await worker!.disconnect();
  41. releaseListeners();
  42. }
  43. }
  44. void releaseListeners() {
  45. worker!.connectErrorEvent.removeListener(_onConnectFail);
  46. worker!.connectedEvent.removeListener(_onConnectSuccess);
  47. worker!.successEvent.removeListener(_onSuccess);
  48. worker!.disconnectedEvent.removeListener(_onDisconnected);
  49. }
  50. @override
  51. void dispose() {
  52. disconnect();
  53. worker?.dispose();
  54. super.dispose();
  55. }
  56. void loadListeners() {
  57. worker!.successEvent.addListener(_onSuccess);
  58. worker!.connectErrorEvent.addListener(_onConnectFail);
  59. worker!.connectedEvent.addListener(_onConnectSuccess);
  60. worker!.disconnectedEvent.addListener(_onDisconnected);
  61. worker!.connect();
  62. }
  63. Future<void> currentDevice() async {
  64. DeviceModel? device = await controller.getDevice(DeviceTypes.WEIGHT);
  65. if (device.isNull) {
  66. _connectStatus = WorkerStatus.unboundDevice;
  67. worker = null;
  68. setState(() {});
  69. return;
  70. }
  71. bmi = BmiDeviceController(device?.model ?? '', device?.mac ?? '');
  72. worker = bmi.worker;
  73. _connectStatus = bmi.connectStatus;
  74. loadListeners();
  75. }
  76. Future<void> initBmi() async {
  77. currentDevice();
  78. await initData();
  79. }
  80. Future<void> initData() async {
  81. await controller.readCachedRecord();
  82. if (controller.diagnosisDataValue['BMI'] == null) {
  83. controller.diagnosisDataValue['BMI'] = {};
  84. }
  85. _weight = controller.diagnosisDataValue['BMI']?['Weight'] ?? '';
  86. _height = controller.diagnosisDataValue['BMI']?['Height'] ?? '';
  87. _bmi = controller.diagnosisDataValue['BMI']?['Bmi'] ?? '';
  88. setState(() {});
  89. }
  90. void _onSuccess(_, double e) {
  91. setState(() {
  92. _weight = e.toString();
  93. controller.saveCachedRecord();
  94. _connectStatus = WorkerStatus.connected;
  95. });
  96. }
  97. void _onDeviceCloseSuccess(sender, e) {
  98. if (e) {
  99. connect();
  100. }
  101. }
  102. void _onConnectFail(sender, e) {
  103. print('连接设备失败');
  104. if (errorCount < 3) {
  105. worker?.connect();
  106. }
  107. setState(() {
  108. errorCount++;
  109. _connectStatus = WorkerStatus.connectionFailed;
  110. });
  111. }
  112. void _onDisconnected(sender, e) {
  113. print('设备连接中断');
  114. if (errorCount < 3) {
  115. worker?.connect();
  116. }
  117. setState(() {
  118. errorCount++;
  119. _connectStatus = WorkerStatus.disconnected;
  120. });
  121. }
  122. void _onConnectSuccess(sender, e) {
  123. _connectStatus = WorkerStatus.connected;
  124. setState(() {});
  125. }
  126. @override
  127. Widget build(BuildContext context) {
  128. return ExamCard(
  129. titleText: const SizedBox(),
  130. // clickCard: () {},
  131. content: Column(
  132. mainAxisAlignment: MainAxisAlignment.start,
  133. children: [
  134. SideBar(
  135. title: '身高',
  136. value: _height.isEmpty ? '--' : _height,
  137. unit: 'cm',
  138. onTap: _inputHeight,
  139. ),
  140. const Divider(indent: 30),
  141. Stack(
  142. children: [
  143. SideBar(
  144. title: '体重',
  145. value: _weight.isEmpty ? '--' : _weight,
  146. hasDevice: true,
  147. unit: 'kg',
  148. onTap: _inputWeight,
  149. ),
  150. if (errorCount < 3)
  151. DeviceStatusPosition(
  152. deviceStatus: DeviceStatus(connectStatus: _connectStatus),
  153. ),
  154. if (errorCount >= 3) _buildErrorButton(),
  155. ],
  156. ),
  157. const Divider(indent: 30),
  158. SideBar(
  159. title: 'BMI',
  160. value: _bmi.isEmpty ? '--' : _bmi,
  161. unit: 'kg/m²',
  162. ),
  163. ],
  164. ));
  165. }
  166. Widget _buildErrorButton() {
  167. return DeviceStatusPosition(
  168. deviceStatus: Row(
  169. children: [
  170. const Text(
  171. '请确认设备是否启动',
  172. style: TextStyle(fontSize: 24, color: Colors.red),
  173. ),
  174. const SizedBox(
  175. width: 8,
  176. ),
  177. IconButton(
  178. onPressed: () {
  179. worker?.connect();
  180. setState(() {
  181. _connectStatus = WorkerStatus.connecting;
  182. errorCount = 0;
  183. });
  184. },
  185. icon: const Icon(Icons.refresh),
  186. iconSize: 32,
  187. ),
  188. const SizedBox(
  189. width: 32,
  190. ),
  191. ],
  192. ),
  193. );
  194. }
  195. Future<void> _inputWeight() async {
  196. String? result = await VDialogNumber(
  197. title: '体重',
  198. initialValue: _weight,
  199. ).show();
  200. if (result?.isNotEmpty ?? false) {
  201. _weight = result ?? '';
  202. getBmi();
  203. controller.diagnosisDataValue['BMI']?['Weight'] = _weight;
  204. controller.diagnosisDataValue['BMI']?['Bmi'] = _bmi;
  205. controller.saveCachedRecord();
  206. }
  207. setState(() {});
  208. }
  209. void getBmi() {
  210. if (_weight.isNotEmpty && _height.isNotEmpty) {
  211. _bmi = (double.parse(_weight) /
  212. ((double.parse(_height) / 100) * (double.parse(_height) / 100)))
  213. .toStringAsFixed(2);
  214. controller.saveCachedRecord();
  215. }
  216. }
  217. Future<void> _inputHeight() async {
  218. String? result = await VDialogNumber(
  219. title: '身高',
  220. initialValue: _height,
  221. ).show();
  222. if (result?.isNotEmpty ?? false) {
  223. _height = result ?? '';
  224. getBmi();
  225. controller.diagnosisDataValue['BMI']?['Height'] = _height;
  226. controller.diagnosisDataValue['BMI']?['Bmi'] = _bmi;
  227. controller.saveCachedRecord();
  228. }
  229. setState(() {});
  230. }
  231. }