sp_o2.dart 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vnote_device_plugin/devices/sp_o2.dart';
  4. import 'package:vnote_device_plugin/models/exams/sp_o2.dart';
  5. class SpO2Card extends StatefulWidget {
  6. final String mac;
  7. final String model;
  8. const SpO2Card({super.key, required this.mac, required this.model});
  9. @override
  10. State<StatefulWidget> createState() => _SpO2CardState();
  11. }
  12. class _SpO2CardState extends State<SpO2Card> {
  13. late final SpO2DeviceWorker worker = SpO2DeviceWorker(
  14. mac: widget.mac,
  15. model: widget.model,
  16. );
  17. SpO2ExamValue? value;
  18. bool _working = false;
  19. @override
  20. void initState() {
  21. worker.valueUpdateEvent.addListener(_onSuccess);
  22. worker.errorEvent.addListener(_onError);
  23. super.initState();
  24. // WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  25. // if (mounted) {
  26. // Future.delayed(const Duration(seconds: 1), () async {
  27. // await worker.connect();
  28. // setState(() {
  29. // _working = true;
  30. // });
  31. // });
  32. // }
  33. // });
  34. }
  35. @override
  36. void dispose() {
  37. worker.valueUpdateEvent.removeListener(_onSuccess);
  38. worker.errorEvent.removeListener(_onError);
  39. worker.disconnect();
  40. super.dispose();
  41. }
  42. void _onSuccess(_, SpO2ExamValue e) {
  43. setState(() {
  44. value = e;
  45. });
  46. }
  47. void _onError(_, String e) {
  48. Get.snackbar(
  49. "提示",
  50. "测量错误: $e",
  51. snackPosition: SnackPosition.TOP,
  52. );
  53. }
  54. @override
  55. Widget build(BuildContext context) {
  56. return Card(
  57. elevation: 4,
  58. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
  59. child: Container(
  60. alignment: Alignment.center,
  61. width: 360,
  62. height: 160,
  63. child: SizedBox(
  64. height: 80,
  65. child: Row(
  66. mainAxisAlignment: MainAxisAlignment.center,
  67. crossAxisAlignment: CrossAxisAlignment.end,
  68. children: [
  69. Text(
  70. value?.spO2.toString() ?? "__",
  71. style: const TextStyle(fontSize: 80),
  72. ),
  73. const SizedBox(width: 8),
  74. const Text(
  75. "%",
  76. style: TextStyle(fontSize: 26),
  77. ),
  78. const SizedBox(width: 24),
  79. SizedBox(
  80. width: 60,
  81. height: 60,
  82. child: _working
  83. ? OutlinedButton(
  84. style: ButtonStyle(
  85. backgroundColor: MaterialStatePropertyAll(
  86. Colors.red.withOpacity(.08),
  87. ),
  88. shape: MaterialStatePropertyAll(
  89. RoundedRectangleBorder(
  90. borderRadius: BorderRadius.circular(30),
  91. ),
  92. ),
  93. ),
  94. onPressed: () async {
  95. await worker.disconnect();
  96. setState(() {
  97. _working = false;
  98. });
  99. },
  100. child: const Text(
  101. "停止",
  102. style: TextStyle(color: Colors.red),
  103. ),
  104. )
  105. : OutlinedButton(
  106. style: ButtonStyle(
  107. backgroundColor: MaterialStatePropertyAll(
  108. Theme.of(context).primaryColor.withOpacity(.08),
  109. ),
  110. shape: MaterialStatePropertyAll(
  111. RoundedRectangleBorder(
  112. borderRadius: BorderRadius.circular(30),
  113. ),
  114. ),
  115. ),
  116. onPressed: () async {
  117. await worker.connect();
  118. setState(() {
  119. _working = true;
  120. });
  121. },
  122. child: const Text("开始"),
  123. ),
  124. ),
  125. ],
  126. ),
  127. ),
  128. ),
  129. );
  130. }
  131. }