import 'package:flutter/material.dart'; import 'package:chicken_farm/services/pda_service.dart'; class PdaDemoPage extends StatefulWidget { const PdaDemoPage({Key? key}) : super(key: key); @override State createState() => _PdaDemoPageState(); } class _PdaDemoPageState extends State { final PdaService _pdaService = PdaService(); String? _scanResult; String? _rfidResult; bool _isScanning = false; @override void initState() { super.initState(); // 开始监听扫描结果 _pdaService.startListeningScanResults(); // 监听扫描结果流 _pdaService.scanResults.listen((result) { setState(() { _scanResult = result; _isScanning = false; }); }); } @override void dispose() { // 停止监听扫描结果 _pdaService.stopListeningScanResults(); super.dispose(); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('PDA功能演示'), ), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // 扫描功能区域 Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text( '条码扫描', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox(height: 16), ElevatedButton( onPressed: _isScanning ? null : () { setState(() { _isScanning = true; _scanResult = null; }); _pdaService.scanBarcode(); }, child: _isScanning ? const Row( mainAxisAlignment: MainAxisAlignment.center, children: [ SizedBox( width: 20, height: 20, child: CircularProgressIndicator( strokeWidth: 2, valueColor: AlwaysStoppedAnimation(Colors.white), ), ), SizedBox(width: 10), Text('扫描中...') ], ) : const Text('触发扫描'), ), const SizedBox(height: 16), const Text('扫描结果:'), const SizedBox(height: 8), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(4), ), child: Text( _scanResult ?? '暂无扫描结果', style: TextStyle( color: _scanResult == null ? Colors.grey : Colors.black, ), ), ), ], ), ), ), const SizedBox(height: 20), // RFID功能区域 Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ const Text( 'RFID读写', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), const SizedBox(height: 16), ElevatedButton( onPressed: () async { setState(() { _rfidResult = null; }); final rfid = await _pdaService.readRfid(); setState(() { _rfidResult = rfid; }); }, child: const Text('读取RFID'), ), const SizedBox(height: 16), ElevatedButton( onPressed: () async { setState(() { _rfidResult = null; }); final result = await _pdaService.writeRfid('TestData123'); setState(() { _rfidResult = result; }); }, child: const Text('写入RFID'), ), const SizedBox(height: 16), const Text('RFID结果:'), const SizedBox(height: 8), Container( padding: const EdgeInsets.all(12), decoration: BoxDecoration( border: Border.all(color: Colors.grey), borderRadius: BorderRadius.circular(4), ), child: Text( _rfidResult ?? '暂无RFID结果', style: TextStyle( color: _rfidResult == null ? Colors.grey : Colors.black, ), ), ), ], ), ), ), ], ), ), ); } }