twelve_heart.dart 3.7 KB

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