123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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<EasyRefreshController> ctrlList;
- late List<SampleTaskListReq> reqList;
- late List<ValueNotifier<DataStatusModel<List<SampleTaskItem>>>> pageStateList;
- ValueNotifier<bool> showPrintBtn = ValueNotifier(false);
- SampleListVM() {
- pageStateList = [
- DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
- DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
- DataStatusModel<List<SampleTaskItem>>().notifier<DataStatusModel<List<SampleTaskItem>>>(),
- ];
- 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),
- ];
- }
- // 显示隐藏批量打印按钮
- void showPrint(){
- showPrintBtn.value = !showPrintBtn.value;
- }
- /// 当前页面
- 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<SampleTaskItem> 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();
- }
- }
|