| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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<PdaDemoPage> createState() => _PdaDemoPageState();
- }
- class _PdaDemoPageState extends State<PdaDemoPage> {
- 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<Color>(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,
- ),
- ),
- ),
- ],
- ),
- ),
- ),
- ],
- ),
- ),
- );
- }
- }
|