weight_height.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import 'package:flutter/material.dart';
  2. import 'package:get/get.dart';
  3. import 'package:vnote_device_plugin/devices/weight_height.dart';
  4. import 'package:vnote_device_plugin/models/exams/weight_height.dart';
  5. class WeightHeightCard extends StatefulWidget {
  6. final String mac;
  7. final String model;
  8. const WeightHeightCard({super.key, required this.mac, required this.model});
  9. @override
  10. State<WeightHeightCard> createState() => _WeightHeightCardState();
  11. }
  12. class _WeightHeightCardState extends State<WeightHeightCard> {
  13. late final WeightHeightDeviceWorker worker =
  14. WeightHeightDeviceWorker(mac: widget.mac, model: widget.model);
  15. WeightHeightExamData? value;
  16. bool _working = false;
  17. @override
  18. void initState() {
  19. worker.successEvent.addListener(_onSuccess);
  20. worker.errorEvent.addListener(_onError);
  21. super.initState();
  22. }
  23. void _onSuccess(_, WeightHeightExamData e) {
  24. setState(() {
  25. value = e;
  26. });
  27. }
  28. void _onError(_, String e) {
  29. Get.snackbar(
  30. "提示",
  31. "测量错误:$e",
  32. snackPosition: SnackPosition.TOP,
  33. );
  34. }
  35. @override
  36. void dispose() {
  37. worker.successEvent.removeListener(_onSuccess);
  38. worker.errorEvent.removeListener(_onError);
  39. worker.disconnect();
  40. super.dispose();
  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: 240,
  51. child: SizedBox(
  52. height: 100,
  53. child: Row(
  54. mainAxisAlignment: MainAxisAlignment.center,
  55. crossAxisAlignment: CrossAxisAlignment.end,
  56. children: [
  57. Center(
  58. child: Column(
  59. children: [
  60. Row(
  61. children: [
  62. Text(
  63. "${value != null ? double.parse(value!.height.toStringAsFixed(1)) : ""}",
  64. style: const TextStyle(fontSize: 40),
  65. ),
  66. const SizedBox(
  67. width: 8,
  68. ),
  69. const Text(
  70. "cm",
  71. style: TextStyle(
  72. fontSize: 20,
  73. ),
  74. ),
  75. ],
  76. ),
  77. Row(
  78. children: [
  79. Text(
  80. "${value != null ? double.parse(value!.weight.toStringAsFixed(1)) : ""}",
  81. style: const TextStyle(fontSize: 40),
  82. ),
  83. const SizedBox(
  84. width: 8,
  85. ),
  86. const Text(
  87. "kg",
  88. style: TextStyle(
  89. fontSize: 20,
  90. ),
  91. ),
  92. ],
  93. ),
  94. ],
  95. ),
  96. ),
  97. const SizedBox(
  98. width: 24,
  99. ),
  100. SizedBox(
  101. width: 60,
  102. height: 60,
  103. child: _working
  104. ? OutlinedButton(
  105. style: ButtonStyle(
  106. backgroundColor: MaterialStatePropertyAll(
  107. Colors.red.withOpacity(.08),
  108. ),
  109. shape: MaterialStatePropertyAll(
  110. RoundedRectangleBorder(
  111. borderRadius: BorderRadius.circular(30),
  112. ),
  113. ),
  114. ),
  115. onPressed: () async {
  116. await worker.disconnect();
  117. setState(() {
  118. _working = false;
  119. });
  120. },
  121. child: const Text(
  122. '停止',
  123. style: TextStyle(color: Colors.red),
  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. setState(() {
  139. _working = true;
  140. });
  141. },
  142. child: const Text("开始测量"),
  143. ),
  144. ),
  145. ],
  146. ),
  147. ),
  148. ),
  149. );
  150. }
  151. }