individual_weighing_page.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. import 'package:chicken_farm/components/vb_rfid_field.dart';
  5. class IndividualWeighingPage extends StatefulWidget {
  6. const IndividualWeighingPage({super.key});
  7. @override
  8. State<IndividualWeighingPage> createState() => _IndividualWeighingPageState();
  9. }
  10. class _IndividualWeighingPageState extends State<IndividualWeighingPage> {
  11. String? _rfid;
  12. double? _weight;
  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
  34. ? _handleSubmit
  35. : null,
  36. style: ElevatedButton.styleFrom(
  37. backgroundColor: _rfid != null && _weight != null
  38. ? Colors.blue
  39. : Colors.grey,
  40. foregroundColor: Colors.white,
  41. ),
  42. child: const Text('提交'),
  43. ),
  44. ),
  45. ],
  46. ),
  47. ),
  48. );
  49. }
  50. Widget _buildRfidSection() {
  51. return VberRfidField(
  52. rfid: _rfid,
  53. onRfidScanned: (rfid) {
  54. setState(() {
  55. _rfid = rfid;
  56. });
  57. ToastUtil.success('电子编号识别成功');
  58. },
  59. label: '电子编号',
  60. placeholder: '未识别',
  61. );
  62. }
  63. Widget _buildWeightSection() {
  64. return Column(
  65. crossAxisAlignment: CrossAxisAlignment.start,
  66. children: [
  67. const Text('重量(kg)', style: TextStyle(fontWeight: FontWeight.bold)),
  68. const SizedBox(height: 10),
  69. Container(
  70. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  71. decoration: BoxDecoration(
  72. border: Border.all(color: Colors.grey),
  73. borderRadius: BorderRadius.circular(8),
  74. ),
  75. child: Row(
  76. children: [
  77. Expanded(
  78. child: Text(
  79. _weight != null ? _weight!.toStringAsFixed(2) : '未读取',
  80. style: TextStyle(
  81. color: _weight != null ? Colors.black : Colors.grey,
  82. fontSize: 16,
  83. ),
  84. ),
  85. ),
  86. IconButton(
  87. icon: _isReadingWeight
  88. ? const SizedBox(
  89. width: 20,
  90. height: 20,
  91. child: CircularProgressIndicator(strokeWidth: 2),
  92. )
  93. : const Icon(Icons.sync, size: 20),
  94. onPressed: _simulateReadWeight,
  95. ),
  96. ],
  97. ),
  98. ),
  99. ],
  100. );
  101. }
  102. // 模拟读取重量
  103. void _simulateReadWeight() {
  104. if (_isReadingWeight) return;
  105. setState(() {
  106. _isReadingWeight = true;
  107. });
  108. // 模拟读取重量过程
  109. Future.delayed(const Duration(seconds: 1), () {
  110. if (mounted) {
  111. setState(() {
  112. _weight = double.parse(
  113. (5.0 + DateTime.now().millisecond % 50 / 10).toStringAsFixed(2),
  114. );
  115. _isReadingWeight = false;
  116. });
  117. ToastUtil.success('重量读取成功');
  118. }
  119. });
  120. }
  121. // 提交数据
  122. void _handleSubmit() {
  123. // 在实际应用中,这里会发送数据到服务器
  124. ScaffoldMessenger.of(context).showSnackBar(
  125. const SnackBar(content: Text('个体称重提交成功'), backgroundColor: Colors.green),
  126. );
  127. // 提交后重置表单
  128. setState(() {
  129. _rfid = null;
  130. _weight = null;
  131. });
  132. }
  133. }