sample_task_list_page.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import 'package:easy_refresh/easy_refresh.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:lszlgl/base/base_lifecycle_state.dart';
  4. import 'package:lszlgl/base/base_vm.dart';
  5. import 'package:lszlgl/config/colors.dart';
  6. import 'package:lszlgl/model/rsp/sample_task_rsp.dart';
  7. import 'package:lszlgl/network/my_api.dart';
  8. import 'package:lszlgl/page/sample_task/sample_list_vm.dart';
  9. import 'package:lszlgl/page/sample_task/sample_task_list_tab_page.dart';
  10. import 'package:lszlgl/service/dict_service.dart';
  11. import 'package:lszlgl/utils/date_time_utils.dart';
  12. import 'package:lszlgl/utils/inject.dart';
  13. import 'package:lszlgl/widget/button.dart';
  14. import 'package:lszlgl/widget/page_widget.dart';
  15. import 'reap_sample_detail/reap_sample_task_page.dart';
  16. import 'stock_sample_detail/stock_sample_task_page.dart';
  17. /// 扦样环节列表
  18. class SampleTaskListPage extends StatefulWidget {
  19. final StepType type;
  20. final int tabIndex;
  21. const SampleTaskListPage({
  22. super.key,
  23. required this.type,
  24. required this.tabIndex,
  25. });
  26. @override
  27. State<SampleTaskListPage> createState() => _SampleTaskListPageState();
  28. }
  29. class _SampleTaskListPageState extends BaseLifecycleState<SampleTaskListPage> with AutomaticKeepAliveClientMixin {
  30. late SampleListVM vm;
  31. /// 详情
  32. void startTaskDetail(bool detail, SampleTaskItem data) async {
  33. bool? success;
  34. switch (widget.type) {
  35. case StepType.reap:
  36. success = await MyRouter.startReapSampleTask(args: ReapSampleTaskPageArgs(detail: detail, id: data.id));
  37. break;
  38. case StepType.stock:
  39. success = await MyRouter.startStockSampleTask(args: StockSampleTaskPageArgs(detail: detail, id: data.id));
  40. break;
  41. }
  42. if (success ?? false) {
  43. vm.refreshAll();
  44. }
  45. }
  46. /// 领取扦样任务
  47. void receiveTask(SampleTaskItem data) async {
  48. MyNavigator.showLoading();
  49. try {
  50. var rsp = await MyApi.get().receiveSampleTaskSgjc(data.id ?? 0);
  51. MyNavigator.dismissLoading();
  52. if (rsp.data == 1) {
  53. MyNavigator.showToast('领取成功');
  54. vm.refreshAll();
  55. }
  56. } catch (e) {
  57. MyNavigator.dismissLoading();
  58. }
  59. }
  60. @override
  61. void onInit() {
  62. vm = Inject.get<SampleListVM>()!;
  63. }
  64. @override
  65. void onFirstShow(Duration timeStamp) {
  66. vm.refresh(widget.tabIndex);
  67. }
  68. @override
  69. Widget build(BuildContext context) {
  70. super.build(context);
  71. return Container(
  72. clipBehavior: Clip.hardEdge,
  73. decoration: const BoxDecoration(
  74. color: Colors.white,
  75. borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
  76. ),
  77. alignment: Alignment.center,
  78. child: buildBody(),
  79. );
  80. }
  81. Widget buildBody() {
  82. return EasyRefresh.builder(
  83. controller: vm.ctrlList[widget.tabIndex],
  84. onRefresh: () => vm.getData(widget.tabIndex, refresh: true),
  85. onLoad: () => vm.getData(widget.tabIndex, refresh: false),
  86. childBuilder: (_, physics) => buildList(physics),
  87. );
  88. }
  89. Widget buildList(ScrollPhysics physics) {
  90. var sliver = vm.pageStateList[widget.tabIndex].builder((v) {
  91. var list = v.data;
  92. if (v.status == DataStatus.error) {
  93. // 加载失败
  94. return SliverToBoxAdapter(child: PageLoadingWidget.error(onTap: () => vm.refresh(widget.tabIndex)));
  95. } else if (list == null || list.isEmpty) {
  96. // 无数据
  97. return const SliverToBoxAdapter(child: PageLoadingWidget.empty());
  98. } else {
  99. return SliverList.builder(
  100. itemCount: list.length,
  101. itemBuilder: (_, index) => buildItem(index, list[index]),
  102. );
  103. }
  104. });
  105. return CustomScrollView(
  106. physics: physics,
  107. slivers: [sliver],
  108. );
  109. }
  110. Widget buildItem(int index, SampleTaskItem item) {
  111. return GestureDetector(
  112. behavior: HitTestBehavior.opaque,
  113. onTap: () {
  114. if (item.deliveryStatus != 2) return;
  115. startTaskDetail(true, item);
  116. },
  117. child: Container(
  118. margin: const EdgeInsets.only(left: 12, right: 12, top: 12),
  119. decoration: const BoxDecoration(
  120. color: Color(0xFFF5FFFD),
  121. borderRadius: BorderRadius.all(Radius.circular(8)),
  122. ),
  123. child: Column(
  124. crossAxisAlignment: CrossAxisAlignment.start,
  125. children: [
  126. const SizedBox(height: 15),
  127. buildTop(item.getDeliveryStatusText()),
  128. buildNumber(item.qyrwdh ?? ''),
  129. buildGrid(item),
  130. buildBottom(item),
  131. const SizedBox(height: 15),
  132. ],
  133. ),
  134. ),
  135. );
  136. }
  137. Widget buildTop(String state) {
  138. return Row(
  139. children: [
  140. buildVLine(),
  141. const Expanded(
  142. child: Text(
  143. '样品单号',
  144. style: TextStyle(color: MyColor.c_333333, fontSize: 14, fontWeight: FontWeight.w500),
  145. ),
  146. ),
  147. buildState(state),
  148. ],
  149. );
  150. }
  151. Widget buildVLine() {
  152. return Container(
  153. width: 4,
  154. height: 18,
  155. margin: const EdgeInsets.only(right: 8),
  156. decoration: const BoxDecoration(
  157. color: MyColor.c_25A6EE,
  158. borderRadius: BorderRadius.all(Radius.circular(2)),
  159. ),
  160. );
  161. }
  162. Widget buildState(String state) {
  163. return Container(
  164. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
  165. decoration: BoxDecoration(
  166. color: MyColor.c_25A6EE.withOpacity(0.1),
  167. borderRadius: const BorderRadius.horizontal(left: Radius.circular(100)),
  168. ),
  169. child: Text(
  170. '状态:$state',
  171. style: const TextStyle(color: MyColor.c_1383C2, fontSize: 13),
  172. ),
  173. );
  174. }
  175. Widget buildNumber(String number) {
  176. return Padding(
  177. padding: const EdgeInsets.only(left: 8, top: 4, bottom: 8),
  178. child: Text(
  179. number,
  180. style: const TextStyle(color: MyColor.c_333333, fontSize: 14, fontWeight: FontWeight.w500),
  181. ),
  182. );
  183. }
  184. Widget buildGrid(SampleTaskItem item) {
  185. List<Map<String, String?>> infoList = [];
  186. if (item.deliveryStatus != 2) {
  187. // 未扦样
  188. infoList.add({'采样品种': item.cypzName});
  189. if (item.jyzb != null) {
  190. infoList.add({'检验指标': item.jyzb});
  191. }
  192. infoList.add({'扦样地区': item.qydq});
  193. if (item.ypdj != null) {
  194. infoList.add({'样品层级': DictService.getDict(DictType.ypdj, value: item.ypdj)?.label});
  195. }
  196. } else {
  197. // 已扦样
  198. infoList.addAll([
  199. {'采样品种': item.cypzName},
  200. {'扦样地区': item.qydq},
  201. ]);
  202. if (item.ypdj != null) {
  203. infoList.add({'样品层级': DictService.getDict(DictType.ypdj, value: item.ypdj)?.label});
  204. }
  205. if (item.jyzb != null) {
  206. infoList.add({'检验指标': item.jyzb});
  207. }
  208. infoList.addAll([
  209. {'扦样人员': item.dgryName},
  210. {'扦样时间': item.qysj},
  211. ]);
  212. }
  213. return Padding(
  214. padding: const EdgeInsets.symmetric(horizontal: 8),
  215. child: LayoutBuilder(builder: (context, constraints) {
  216. return Wrap(
  217. spacing: 4,
  218. runSpacing: 4,
  219. children: infoList.map((item) {
  220. return SizedBox(
  221. width: constraints.maxWidth / 2 - 2,
  222. child: Text(
  223. '${item.keys.first}:${item.values.first ?? ''}',
  224. style: const TextStyle(fontSize: 13, color: MyColor.c_666666),
  225. ),
  226. );
  227. }).toList(),
  228. );
  229. }),
  230. );
  231. }
  232. Widget buildBottom(SampleTaskItem item) {
  233. if (item.deliveryStatus == 2) return const SizedBox.shrink();
  234. Widget button = const SizedBox.shrink();
  235. if (widget.type == StepType.reap && item.deliveryStatus == 0) {
  236. button = MyButton(
  237. '任务领取',
  238. onTap: () => receiveTask(item),
  239. fountSize: 13,
  240. alignment: null,
  241. padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 12),
  242. );
  243. } else {
  244. button = MyButton(
  245. '开始扦样',
  246. onTap: () => startTaskDetail(false, item),
  247. fountSize: 13,
  248. alignment: null,
  249. padding: const EdgeInsets.symmetric(vertical: 6, horizontal: 12),
  250. );
  251. }
  252. return Column(
  253. children: [
  254. buildDivider(),
  255. Row(
  256. children: [
  257. const SizedBox(width: 8),
  258. Expanded(
  259. child: Text(
  260. '扦样单据创建于${DateTimeUtils.yyyymmdd(timestamp: item.createTime) ?? ''}',
  261. style: const TextStyle(fontSize: 13, color: MyColor.c_666666),
  262. ),
  263. ),
  264. const SizedBox(width: 4),
  265. button,
  266. const SizedBox(width: 12),
  267. ],
  268. ),
  269. ],
  270. );
  271. }
  272. Widget buildDivider() {
  273. return Container(
  274. width: double.infinity,
  275. height: 1,
  276. color: MyColor.c_3BD2E5.withOpacity(0.15),
  277. margin: const EdgeInsets.symmetric(vertical: 10),
  278. );
  279. }
  280. @override
  281. bool get wantKeepAlive => true;
  282. }