ic_reader.dart 4.3 KB

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