import 'package:dio/dio.dart'; import 'package:easy_refresh/easy_refresh.dart'; import 'package:flutter/material.dart'; import 'package:lszlgl/base/base_vm.dart'; import 'package:lszlgl/main.dart'; import 'package:lszlgl/model/req/sample_task_list_req.dart'; import 'package:lszlgl/model/rsp/sample_task_rsp.dart'; import '../../network/my_api.dart'; class SampleListVM extends BaseVM { late List ctrlList; late List reqList; late List>>> pageStateList; SampleListVM() { pageStateList = [ DataStatusModel>().notifier>>(), DataStatusModel>().notifier>>(), DataStatusModel>().notifier>>(), ]; ctrlList = [ EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true), EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true), EasyRefreshController(controlFinishRefresh: true, controlFinishLoad: true), ]; reqList = [ SampleTaskListReq(deliveryStatus: 0), SampleTaskListReq(deliveryStatus: 1), SampleTaskListReq(deliveryStatus: 2), ]; } /// 当前页面 final bool currentComplete = false; void refresh(int index) { ctrlList[index].callRefresh(overOffset: 100); } void refreshAll() { refresh(0); refresh(1); refresh(2); } /// 获取数据 /// * [index] 页面索引 /// * [refresh] 是否刷新 true=刷新, false=加载更多 void getData(int index, {bool refresh = true}) async { var req = reqList[index]; var pageState = pageStateList[index]; var ctrl = ctrlList[index]; if (refresh) { req.pageNo = 1; } try { var value = await MyApi.get().sampleTaskList(req.toJson()); if (isDispose) return; List data = value.data?.list ?? []; var list = pageState.value.data ?? []; if (refresh) { list = data; ctrl.finishRefresh(IndicatorResult.success, true); ctrl.resetFooter(); } else { list.addAll(data); } pageState.update(pageState.value.success(data: list)); if (data.isEmpty) { ctrl.finishLoad(IndicatorResult.noMore, true); } else { ctrl.finishLoad(IndicatorResult.success, true); } req.pageNo++; } on DioException catch (err) { logger.e('${err.message}'); if (req.pageNo == 1) pageState.update(pageState.value.error()); if (refresh) { ctrl.finishRefresh(IndicatorResult.fail, true); } else { ctrl.finishLoad(IndicatorResult.fail, true); } } } @override void dispose() { for (var ctrl in ctrlList) { ctrl.dispose(); } for (var pageState in pageStateList) { pageState.dispose(); } super.dispose(); } }