pda_demo_page.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import 'package:flutter/material.dart';
  2. import 'package:chicken_farm/services/pda_service.dart';
  3. class PdaDemoPage extends StatefulWidget {
  4. const PdaDemoPage({Key? key}) : super(key: key);
  5. @override
  6. State<PdaDemoPage> createState() => _PdaDemoPageState();
  7. }
  8. class _PdaDemoPageState extends State<PdaDemoPage> {
  9. final PdaService _pdaService = PdaService();
  10. String? _scanResult;
  11. String? _rfidResult;
  12. bool _isScanning = false;
  13. @override
  14. void initState() {
  15. super.initState();
  16. // 开始监听扫描结果
  17. _pdaService.startListeningScanResults();
  18. // 监听扫描结果流
  19. _pdaService.scanResults.listen((result) {
  20. setState(() {
  21. _scanResult = result;
  22. _isScanning = false;
  23. });
  24. });
  25. }
  26. @override
  27. void dispose() {
  28. // 停止监听扫描结果
  29. _pdaService.stopListeningScanResults();
  30. super.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. appBar: AppBar(
  36. title: const Text('PDA功能演示'),
  37. ),
  38. body: Padding(
  39. padding: const EdgeInsets.all(16.0),
  40. child: Column(
  41. crossAxisAlignment: CrossAxisAlignment.stretch,
  42. children: [
  43. // 扫描功能区域
  44. Card(
  45. child: Padding(
  46. padding: const EdgeInsets.all(16.0),
  47. child: Column(
  48. crossAxisAlignment: CrossAxisAlignment.stretch,
  49. children: [
  50. const Text(
  51. '条码扫描',
  52. style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
  53. ),
  54. const SizedBox(height: 16),
  55. ElevatedButton(
  56. onPressed: _isScanning
  57. ? null
  58. : () {
  59. setState(() {
  60. _isScanning = true;
  61. _scanResult = null;
  62. });
  63. _pdaService.scanBarcode();
  64. },
  65. child: _isScanning
  66. ? const Row(
  67. mainAxisAlignment: MainAxisAlignment.center,
  68. children: [
  69. SizedBox(
  70. width: 20,
  71. height: 20,
  72. child: CircularProgressIndicator(
  73. strokeWidth: 2,
  74. valueColor:
  75. AlwaysStoppedAnimation<Color>(Colors.white),
  76. ),
  77. ),
  78. SizedBox(width: 10),
  79. Text('扫描中...')
  80. ],
  81. )
  82. : const Text('触发扫描'),
  83. ),
  84. const SizedBox(height: 16),
  85. const Text('扫描结果:'),
  86. const SizedBox(height: 8),
  87. Container(
  88. padding: const EdgeInsets.all(12),
  89. decoration: BoxDecoration(
  90. border: Border.all(color: Colors.grey),
  91. borderRadius: BorderRadius.circular(4),
  92. ),
  93. child: Text(
  94. _scanResult ?? '暂无扫描结果',
  95. style: TextStyle(
  96. color: _scanResult == null ? Colors.grey : Colors.black,
  97. ),
  98. ),
  99. ),
  100. ],
  101. ),
  102. ),
  103. ),
  104. const SizedBox(height: 20),
  105. // RFID功能区域
  106. Card(
  107. child: Padding(
  108. padding: const EdgeInsets.all(16.0),
  109. child: Column(
  110. crossAxisAlignment: CrossAxisAlignment.stretch,
  111. children: [
  112. const Text(
  113. 'RFID读写',
  114. style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
  115. ),
  116. const SizedBox(height: 16),
  117. ElevatedButton(
  118. onPressed: () async {
  119. setState(() {
  120. _rfidResult = null;
  121. });
  122. final rfid = await _pdaService.readRfid();
  123. setState(() {
  124. _rfidResult = rfid;
  125. });
  126. },
  127. child: const Text('读取RFID'),
  128. ),
  129. const SizedBox(height: 16),
  130. ElevatedButton(
  131. onPressed: () async {
  132. setState(() {
  133. _rfidResult = null;
  134. });
  135. final result = await _pdaService.writeRfid('TestData123');
  136. setState(() {
  137. _rfidResult = result;
  138. });
  139. },
  140. child: const Text('写入RFID'),
  141. ),
  142. const SizedBox(height: 16),
  143. const Text('RFID结果:'),
  144. const SizedBox(height: 8),
  145. Container(
  146. padding: const EdgeInsets.all(12),
  147. decoration: BoxDecoration(
  148. border: Border.all(color: Colors.grey),
  149. borderRadius: BorderRadius.circular(4),
  150. ),
  151. child: Text(
  152. _rfidResult ?? '暂无RFID结果',
  153. style: TextStyle(
  154. color: _rfidResult == null ? Colors.grey : Colors.black,
  155. ),
  156. ),
  157. ),
  158. ],
  159. ),
  160. ),
  161. ),
  162. ],
  163. ),
  164. ),
  165. );
  166. }
  167. }