weight.dart 4.1 KB

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