urine.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vnote_device_plugin/devices/urine.dart';
  4. import 'package:vnote_device_plugin/models/exams/urine.dart';
  5. class UrineCard extends StatefulWidget {
  6. final String mac;
  7. final String model;
  8. const UrineCard({super.key, required this.mac, required this.model});
  9. @override
  10. State<StatefulWidget> createState() => _UrineCardState();
  11. }
  12. class _UrineCardState extends State<UrineCard> {
  13. late final UrineDeviceWorker worker = UrineDeviceWorker(
  14. mac: widget.mac,
  15. model: widget.model,
  16. );
  17. UrineExamData? value;
  18. bool _working = false;
  19. @override
  20. void initState() {
  21. worker.successEvent.addListener(_onSuccess);
  22. worker.errorEvent.addListener(_onError);
  23. worker.noResultEvent.addListener(_onNoResult);
  24. super.initState();
  25. }
  26. @override
  27. void dispose() {
  28. worker.successEvent.removeListener(_onSuccess);
  29. worker.errorEvent.removeListener(_onError);
  30. worker.noResultEvent.removeListener(_onNoResult);
  31. worker.disconnect();
  32. super.dispose();
  33. }
  34. void _onSuccess(Object _, UrineExamData e) {
  35. setState(() {
  36. value = e;
  37. });
  38. }
  39. void _onError(_, String e) {
  40. setState(() {
  41. value = null;
  42. });
  43. Get.snackbar(
  44. "提示",
  45. "测量错误: $e",
  46. snackPosition: SnackPosition.TOP,
  47. );
  48. }
  49. void _onNoResult(_, void e) {
  50. Get.snackbar(
  51. "提示",
  52. "测量无结果",
  53. snackPosition: SnackPosition.TOP,
  54. );
  55. }
  56. @override
  57. Widget build(BuildContext context) {
  58. const textStyle = TextStyle(fontSize: 14);
  59. return Card(
  60. elevation: 4,
  61. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  62. child: Container(
  63. alignment: Alignment.center,
  64. width: 360,
  65. height: 160,
  66. child: SizedBox(
  67. height: 120,
  68. child: Row(
  69. mainAxisAlignment: MainAxisAlignment.center,
  70. crossAxisAlignment: CrossAxisAlignment.end,
  71. children: [
  72. if (value != null) ...[
  73. Column(
  74. mainAxisAlignment: MainAxisAlignment.start,
  75. crossAxisAlignment: CrossAxisAlignment.start,
  76. children: [
  77. Text("LEU: ${value!.leu}", style: textStyle),
  78. Text("BLD: ${value!.bld}", style: textStyle),
  79. Text("NIT: ${value!.nit}", style: textStyle),
  80. Text("KET: ${value!.ket}", style: textStyle),
  81. Text("UBG: ${value!.ubg}", style: textStyle),
  82. Text("BIL: ${value!.bil}", style: textStyle),
  83. ],
  84. ),
  85. const SizedBox(width: 8),
  86. Column(
  87. mainAxisAlignment: MainAxisAlignment.start,
  88. crossAxisAlignment: CrossAxisAlignment.start,
  89. children: [
  90. Text("PRO: ${value!.pro}", style: textStyle),
  91. Text("GLU: ${value!.glu}", style: textStyle),
  92. Text("PH: ${value!.ph}", style: textStyle),
  93. Text("SG: ${value!.sg}", style: textStyle),
  94. Text("VC: ${value!.vc}", style: textStyle),
  95. ],
  96. ),
  97. ],
  98. const SizedBox(width: 24),
  99. SizedBox(
  100. width: 60,
  101. height: 60,
  102. child: _working
  103. ? OutlinedButton(
  104. style: ButtonStyle(
  105. backgroundColor: MaterialStatePropertyAll(
  106. Colors.red.withOpacity(.08),
  107. ),
  108. shape: MaterialStatePropertyAll(
  109. RoundedRectangleBorder(
  110. borderRadius: BorderRadius.circular(30),
  111. ),
  112. ),
  113. ),
  114. onPressed: () async {
  115. await worker.disconnect();
  116. setState(() {
  117. _working = false;
  118. });
  119. },
  120. child: const Text(
  121. "停止",
  122. style: TextStyle(color: Colors.red),
  123. ),
  124. )
  125. : OutlinedButton(
  126. style: ButtonStyle(
  127. backgroundColor: MaterialStatePropertyAll(
  128. Theme.of(context).primaryColor.withOpacity(.08),
  129. ),
  130. shape: MaterialStatePropertyAll(
  131. RoundedRectangleBorder(
  132. borderRadius: BorderRadius.circular(30),
  133. ),
  134. ),
  135. ),
  136. onPressed: () async {
  137. await worker.connect();
  138. worker.autoTest();
  139. setState(() {
  140. _working = true;
  141. });
  142. },
  143. child: const Text("开始"),
  144. ),
  145. ),
  146. ],
  147. ),
  148. ),
  149. ),
  150. );
  151. }
  152. }