individual_culling_page.dart 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import 'package:chicken_farm/apis/index.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:chicken_farm/components/vb_app_bar.dart';
  4. import 'package:chicken_farm/components/vb_dict_select.dart';
  5. import 'package:chicken_farm/core/utils/toast.dart';
  6. import 'package:chicken_farm/components/vb_rfid_field.dart';
  7. class IndividualCullingPage extends StatefulWidget {
  8. const IndividualCullingPage({super.key});
  9. @override
  10. State<IndividualCullingPage> createState() => _IndividualCullingPageState();
  11. }
  12. class _IndividualCullingPageState extends State<IndividualCullingPage> {
  13. String? _rfid;
  14. String? _cullReason;
  15. String? _disposalMethod;
  16. @override
  17. Widget build(BuildContext context) {
  18. return Scaffold(
  19. appBar: const VberAppBar(title: '个体淘汰', showLeftButton: true),
  20. body: Padding(
  21. padding: const EdgeInsets.all(16.0),
  22. child: Column(
  23. crossAxisAlignment: CrossAxisAlignment.start,
  24. children: [
  25. // 电子编号区域
  26. _buildRfidSection(),
  27. const SizedBox(height: 20),
  28. // 淘汰原因
  29. _buildCullReasonSection(),
  30. const SizedBox(height: 20),
  31. // 处置方式
  32. _buildDisposalMethodSection(),
  33. const SizedBox(height: 30),
  34. // 提交按钮
  35. SizedBox(
  36. width: double.infinity,
  37. child: ElevatedButton(
  38. onPressed:
  39. _rfid != null &&
  40. _cullReason != null &&
  41. _disposalMethod != null
  42. ? _handleSubmit
  43. : null,
  44. style: ElevatedButton.styleFrom(
  45. backgroundColor:
  46. _rfid != null &&
  47. _cullReason != null &&
  48. _disposalMethod != null
  49. ? Colors.blue
  50. : Colors.grey,
  51. foregroundColor: Colors.white,
  52. ),
  53. child: const Text('提交'),
  54. ),
  55. ),
  56. ],
  57. ),
  58. ),
  59. );
  60. }
  61. Widget _buildRfidSection() {
  62. return VberRfidField(
  63. rfid: _rfid,
  64. onRfidScanned: (rfid) {
  65. setState(() {
  66. _rfid = rfid;
  67. });
  68. ToastUtil.success('电子编号识别成功');
  69. },
  70. label: '电子编号',
  71. placeholder: '未识别',
  72. );
  73. }
  74. Widget _buildCullReasonSection() {
  75. return Column(
  76. crossAxisAlignment: CrossAxisAlignment.start,
  77. children: [
  78. const Text('淘汰原因', style: TextStyle(fontWeight: FontWeight.bold)),
  79. const SizedBox(height: 10),
  80. Container(
  81. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  82. decoration: BoxDecoration(
  83. border: Border.all(color: Colors.grey),
  84. borderRadius: BorderRadius.circular(8),
  85. ),
  86. child: VberDictSelect(
  87. dictType: 'chicken_cull_reason',
  88. value: _cullReason,
  89. onChanged: (value) {
  90. setState(() {
  91. _cullReason = value;
  92. });
  93. },
  94. hint: '请选择淘汰原因',
  95. hideUnderline: true,
  96. ),
  97. ),
  98. ],
  99. );
  100. }
  101. Widget _buildDisposalMethodSection() {
  102. return Column(
  103. crossAxisAlignment: CrossAxisAlignment.start,
  104. children: [
  105. const Text('处置方式', style: TextStyle(fontWeight: FontWeight.bold)),
  106. const SizedBox(height: 10),
  107. Container(
  108. padding: const EdgeInsets.fromLTRB(16, 2, 16, 2),
  109. decoration: BoxDecoration(
  110. border: Border.all(color: Colors.grey),
  111. borderRadius: BorderRadius.circular(8),
  112. ),
  113. child: VberDictSelect(
  114. dictType: 'chicken_disposal_method',
  115. value: _disposalMethod,
  116. onChanged: (value) {
  117. setState(() {
  118. _disposalMethod = value;
  119. });
  120. },
  121. hint: '请选择处置方式',
  122. hideUnderline: true,
  123. ),
  124. ),
  125. ],
  126. );
  127. }
  128. // 提交数据
  129. void _handleSubmit() {
  130. final data = {
  131. "rfid": _rfid,
  132. "cull_reason": _cullReason,
  133. "disposal_method": _disposalMethod,
  134. };
  135. apis.breeding.submitApi
  136. .cull(data)
  137. .then((_) {
  138. if (mounted) {
  139. ScaffoldMessenger.of(context).showSnackBar(
  140. const SnackBar(
  141. content: Text('淘汰提交成功'),
  142. backgroundColor: Colors.green,
  143. ),
  144. );
  145. // 提交后重置表单
  146. setState(() {
  147. _rfid = null;
  148. _cullReason = null;
  149. _disposalMethod = null;
  150. });
  151. }
  152. })
  153. .catchError((err) {
  154. ToastUtil.error('淘汰提交失败');
  155. if (mounted && err != null) {
  156. String errorMessage = err.toString();
  157. if (err is Exception) {
  158. errorMessage = err.toString();
  159. }
  160. ScaffoldMessenger.of(context).showSnackBar(
  161. SnackBar(
  162. content: Text(errorMessage),
  163. backgroundColor: Colors.red,
  164. ),
  165. );
  166. }
  167. });
  168. }
  169. }