sample_task_list_page.dart 9.4 KB

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