sample_list_vm.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'package:dio/dio.dart';
  2. import 'package:easy_refresh/easy_refresh.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:lszlgl/base/base_vm.dart';
  5. import 'package:lszlgl/main.dart';
  6. import 'package:lszlgl/model/req/sample_task_list_req.dart';
  7. import 'package:lszlgl/model/rsp/sample_task_rsp.dart';
  8. import '../../network/my_api.dart';
  9. class SampleListVM extends BaseVM {
  10. late List<EasyRefreshController> ctrlList;
  11. late List<SampleTaskListReq> reqList;
  12. late List<ValueNotifier<DataStatusModel<List<SampleTaskItem>>>> pageStateList;
  13. ValueNotifier<bool> showPrintBtn = ValueNotifier(false);
  14. SampleListVM() {
  15. pageStateList = [
  16. DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
  17. DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
  18. DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
  19. ];
  20. ctrlList = [
  21. EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true),
  22. EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true),
  23. EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true),
  24. ];
  25. reqList = [
  26. SampleTaskListReq(deliveryStatus: 0),
  27. SampleTaskListReq(deliveryStatus: 1),
  28. SampleTaskListReq(deliveryStatus: 2),
  29. ];
  30. }
  31. // 显示隐藏批量打印按钮
  32. void showPrint(){
  33. showPrintBtn.value = !showPrintBtn.value;
  34. }
  35. /// 当前页面
  36. final bool currentComplete = false;
  37. void refresh(int index) {
  38. ctrlList[index].callRefresh(overOffset: 100);
  39. }
  40. void refreshAll() {
  41. refresh(0);
  42. refresh(1);
  43. refresh(2);
  44. }
  45. /// 获取数据
  46. /// * [index] 页面索引
  47. /// * [refresh] 是否刷新 true=刷新, false=加载更多
  48. void getData(int index, {bool refresh = true}) async {
  49. var req = reqList[index];
  50. var pageState = pageStateList[index];
  51. var ctrl = ctrlList[index];
  52. if (refresh) {
  53. req.pageNo = 1;
  54. }
  55. try {
  56. var value = await MyApi.get().sampleTaskList(req.toJson());
  57. if (isDispose) return;
  58. List<SampleTaskItem> data = value.data?.list ?? [];
  59. var list = pageState.value.data ?? [];
  60. if (refresh) {
  61. list = data;
  62. ctrl.finishRefresh(IndicatorResult.success, true);
  63. ctrl.resetFooter();
  64. } else {
  65. list.addAll(data);
  66. }
  67. pageState.update(pageState.value.success(data: list));
  68. if (data.isEmpty) {
  69. ctrl.finishLoad(IndicatorResult.noMore, true);
  70. } else {
  71. ctrl.finishLoad(IndicatorResult.success, true);
  72. }
  73. req.pageNo++;
  74. } on DioException catch (err) {
  75. logger.e('${err.message}');
  76. if (req.pageNo == 1) pageState.update(pageState.value.error());
  77. if (refresh) {
  78. ctrl.finishRefresh(IndicatorResult.fail, true);
  79. } else {
  80. ctrl.finishLoad(IndicatorResult.fail, true);
  81. }
  82. }
  83. }
  84. @override
  85. void dispose() {
  86. for (var ctrl in ctrlList) {
  87. ctrl.dispose();
  88. }
  89. for (var pageState in pageStateList) {
  90. pageState.dispose();
  91. }
  92. super.dispose();
  93. }
  94. }