individual_weighing_page.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import 'package:flutter/material.dart';
  2. import 'package:chicken_farm/components/vb_app_bar.dart';
  3. import 'package:chicken_farm/core/utils/toast.dart';
  4. class IndividualWeighingPage extends StatefulWidget {
  5. const IndividualWeighingPage({super.key});
  6. @override
  7. State<IndividualWeighingPage> createState() => _IndividualWeighingPageState();
  8. }
  9. class _IndividualWeighingPageState extends State<IndividualWeighingPage> {
  10. String? _rfid;
  11. double? _weight;
  12. bool _isScanningRfid = false;
  13. bool _isReadingWeight = false;
  14. @override
  15. Widget build(BuildContext context) {
  16. return Scaffold(
  17. appBar: const VberAppBar(title: '个体称重', showLeftButton: true),
  18. body: Padding(
  19. padding: const EdgeInsets.all(16.0),
  20. child: Column(
  21. crossAxisAlignment: CrossAxisAlignment.start,
  22. children: [
  23. // 电子编号区域
  24. _buildRfidSection(),
  25. const SizedBox(height: 20),
  26. // 重量区域
  27. _buildWeightSection(),
  28. const SizedBox(height: 30),
  29. // 提交按钮
  30. SizedBox(
  31. width: double.infinity,
  32. child: ElevatedButton(
  33. onPressed: _rfid != null && _weight != null ? _handleSubmit : null,
  34. style: ElevatedButton.styleFrom(
  35. backgroundColor:
  36. _rfid != null && _weight != null ? Colors.blue : Colors.grey,
  37. foregroundColor: Colors.white,
  38. ),
  39. child: const Text('提交'),
  40. ),
  41. ),
  42. ],
  43. ),
  44. ),
  45. );
  46. }
  47. Widget _buildRfidSection() {
  48. return Column(
  49. crossAxisAlignment: CrossAxisAlignment.start,
  50. children: [
  51. const Text('电子编号', style: TextStyle(fontWeight: FontWeight.bold)),
  52. const SizedBox(height: 10),
  53. Container(
  54. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  55. decoration: BoxDecoration(
  56. border: Border.all(color: Colors.grey),
  57. borderRadius: BorderRadius.circular(8),
  58. ),
  59. child: Row(
  60. children: [
  61. Expanded(
  62. child: Text(
  63. _rfid ?? '未扫描',
  64. style: TextStyle(
  65. color: _rfid != null ? Colors.black : Colors.grey,
  66. fontSize: 16,
  67. ),
  68. ),
  69. ),
  70. IconButton(
  71. icon: _isScanningRfid
  72. ? const SizedBox(
  73. width: 20,
  74. height: 20,
  75. child: CircularProgressIndicator(strokeWidth: 2),
  76. )
  77. : const Icon(Icons.qr_code_scanner, size: 20),
  78. onPressed: _simulateScanRfid,
  79. ),
  80. ],
  81. ),
  82. ),
  83. ],
  84. );
  85. }
  86. Widget _buildWeightSection() {
  87. return Column(
  88. crossAxisAlignment: CrossAxisAlignment.start,
  89. children: [
  90. const Text('重量(kg)', style: TextStyle(fontWeight: FontWeight.bold)),
  91. const SizedBox(height: 10),
  92. Container(
  93. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  94. decoration: BoxDecoration(
  95. border: Border.all(color: Colors.grey),
  96. borderRadius: BorderRadius.circular(8),
  97. ),
  98. child: Row(
  99. children: [
  100. Expanded(
  101. child: Text(
  102. _weight != null ? _weight!.toStringAsFixed(2) : '未读取',
  103. style: TextStyle(
  104. color: _weight != null ? Colors.black : Colors.grey,
  105. fontSize: 16,
  106. ),
  107. ),
  108. ),
  109. IconButton(
  110. icon: _isReadingWeight
  111. ? const SizedBox(
  112. width: 20,
  113. height: 20,
  114. child: CircularProgressIndicator(strokeWidth: 2),
  115. )
  116. : const Icon(Icons.sync, size: 20),
  117. onPressed: _simulateReadWeight,
  118. ),
  119. ],
  120. ),
  121. ),
  122. ],
  123. );
  124. }
  125. // 模拟扫描电子编号
  126. void _simulateScanRfid() {
  127. if (_isScanningRfid) return;
  128. setState(() {
  129. _isScanningRfid = true;
  130. });
  131. // 模拟扫描过程
  132. Future.delayed(const Duration(seconds: 1), () {
  133. if (mounted) {
  134. setState(() {
  135. _rfid = 'RFID-${DateTime.now().millisecondsSinceEpoch}';
  136. _isScanningRfid = false;
  137. });
  138. ToastUtil.success('电子编号扫描成功');
  139. }
  140. });
  141. }
  142. // 模拟读取重量
  143. void _simulateReadWeight() {
  144. if (_isReadingWeight) return;
  145. setState(() {
  146. _isReadingWeight = true;
  147. });
  148. // 模拟读取重量过程
  149. Future.delayed(const Duration(seconds: 1), () {
  150. if (mounted) {
  151. setState(() {
  152. _weight = double.parse((5.0 + DateTime.now().millisecond % 50 / 10).toStringAsFixed(2));
  153. _isReadingWeight = false;
  154. });
  155. ToastUtil.success('重量读取成功');
  156. }
  157. });
  158. }
  159. // 提交数据
  160. void _handleSubmit() {
  161. // 在实际应用中,这里会发送数据到服务器
  162. ScaffoldMessenger.of(context).showSnackBar(
  163. const SnackBar(content: Text('个体称重提交成功'), backgroundColor: Colors.green),
  164. );
  165. // 提交后重置表单
  166. setState(() {
  167. _rfid = null;
  168. _weight = null;
  169. });
  170. }
  171. }