sample_list_vm.dart 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. SampleListVM() {
  14. pageStateList = [
  15. DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
  16. DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
  17. DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
  18. ];
  19. ctrlList = [
  20. EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true),
  21. EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true),
  22. EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true),
  23. ];
  24. reqList = [
  25. SampleTaskListReq(deliveryStatus: 0),
  26. SampleTaskListReq(deliveryStatus: 1),
  27. SampleTaskListReq(deliveryStatus: 2),
  28. ];
  29. }
  30. /// 当前页面
  31. final bool currentComplete = false;
  32. void refresh(int index) {
  33. ctrlList[index].callRefresh(overOffset: 100);
  34. }
  35. void refreshAll() {
  36. refresh(0);
  37. refresh(1);
  38. refresh(2);
  39. }
  40. /// 获取数据
  41. /// * [index] 页面索引
  42. /// * [refresh] 是否刷新 true=刷新, false=加载更多
  43. void getData(int index, {bool refresh = true}) async {
  44. var req = reqList[index];
  45. var pageState = pageStateList[index];
  46. var ctrl = ctrlList[index];
  47. if (refresh) {
  48. req.pageNo = 1;
  49. }
  50. try {
  51. var value = await MyApi.get().sampleTaskList(req.toJson());
  52. if (isDispose) return;
  53. List<SampleTaskItem> data = value.data?.list ?? [];
  54. var list = pageState.value.data ?? [];
  55. if (refresh) {
  56. list = data;
  57. ctrl.finishRefresh(IndicatorResult.success, true);
  58. ctrl.resetFooter();
  59. } else {
  60. list.addAll(data);
  61. }
  62. pageState.update(pageState.value.success(data: list));
  63. if (data.isEmpty) {
  64. ctrl.finishLoad(IndicatorResult.noMore, true);
  65. } else {
  66. ctrl.finishLoad(IndicatorResult.success, true);
  67. }
  68. req.pageNo++;
  69. } on DioException catch (err) {
  70. logger.e('${err.message}');
  71. if (req.pageNo == 1) pageState.update(pageState.value.error());
  72. if (refresh) {
  73. ctrl.finishRefresh(IndicatorResult.fail, true);
  74. } else {
  75. ctrl.finishLoad(IndicatorResult.fail, true);
  76. }
  77. }
  78. }
  79. @override
  80. void dispose() {
  81. for (var ctrl in ctrlList) {
  82. ctrl.dispose();
  83. }
  84. for (var pageState in pageStateList) {
  85. pageState.dispose();
  86. }
  87. super.dispose();
  88. }
  89. }