heart.dart 3.7 KB

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