123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- import 'package:domain/entity/rewards_list_entity.dart';
- import 'package:domain/entity/rewards_search_entity.dart';
- import 'package:flutter/material.dart';
- import 'package:plugin_platform/http/http_result.dart';
- import 'package:riverpod_annotation/riverpod_annotation.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:plugin_platform/engine/toast/toast_engine.dart';
- import 'package:widgets/load_state_layout.dart';
- import 'package:widgets/picker/option_pick_util.dart';
- import 'package:widgets/widget_export.dart';
- import './rewards_search_state.dart';
- import './rewards_search_repository.dart';
- part 'rewards_search_vm.g.dart';
- @riverpod
- class RewardsSearchVm extends _$RewardsSearchVm {
- late RewardsSearchRepository rewardsSearchRepository;
- var page = 1;
- var limit = 10;
- bool _needShowPlaceholder = false; //是否展示LoadingView
- // Refresh 控制器
- final EasyRefreshController refreshController = EasyRefreshController(
- controlFinishRefresh: true, //允许刷新
- controlFinishLoad: true, //允许加载
- );
- RewardsSearchState initState() {
- return RewardsSearchState(list: []);
- }
- @override
- RewardsSearchState build() {
- // 引入数据仓库
- rewardsSearchRepository = ref.read(rewardsSearchRepositoryProvider);
- // 初始化状态
- RewardsSearchState state = initState();
- // 初始化列表数据
- return state;
- }
- // 初始化页面数据
- initPageData() {
- getListData();
- }
- // 重试请求
- Future retryRequest({int? id}) async {
- _needShowPlaceholder = true;
- getListData();
- }
- //刷新页面状态
- void changeLoadingState(LoadState loadState, String? errorMsg) {
- state = state.copyWith(loadingState: loadState, errorMessage: errorMsg);
- }
- // 获取list 列表数据
- void getListData<T>() async {
- Log.d("加载listData数据---------------start-----");
- try {
- //请求网络
- Map<String, dynamic> params = {};
- Log.d("请求参数------$params");
- final result =
- await rewardsSearchRepository.fetchPropertyNewsList(params);
- Log.d("请求完成结果------${result.data}");
- //校验成功失败
- if (result.isSuccess) {
- state = state.copyWith(
- detailInfo: result.data as RewardsSearchEntity,
- );
- Log.d("123------${state.detailInfo}");
- changeLoadingState(LoadState.State_Success, null);
- } else {
- String errorMessage = result.errorMsg!;
- changeLoadingState(LoadState.State_Error, errorMessage);
- ToastEngine.show(result.errorMsg ?? "Network Load Error");
- }
- } catch (e) {
- ToastEngine.show("Error: $e");
- }
- }
- void searchIn(value) {
- ToastEngine.show('$value');
- state = state.copyWith(
- keyword: value,
- );
- page = 1;
- getList();
- }
- // 上拉加载 更多
- Future loadMore() async {
- page++;
- getList();
- }
- // 下拉刷新
- Future onRefresh() async {
- page = 1;
- getList();
- }
- // 获取list 列表数据
- Future getList<T>() async {
- // if (_needShowPlaceholder) {
- // changeLoadingState(LoadState.State_Loading, null);
- // }
- Log.d("加载listData数据---------------start--${page}---");
- try {
- //请求网络
- Map<String, dynamic> params = {
- "page": page,
- "limit": limit,
- "keyword": state.keyword,
- };
- Log.d("请求参数------$params");
- final result = await rewardsSearchRepository.fetchList(params);
- //校验成功失败
- if (result.isSuccess) {
- handlerResultList((result.data as RewardsListEntity).list);
- state = state.copyWith(
- searchIs: false,
- );
- } else {
- String errorMessage = result.errorMsg!;
- changeLoadingState(LoadState.State_Error, errorMessage);
- ToastEngine.show(result.errorMsg ?? "Network Load Error");
- }
- } catch (e) {
- ToastEngine.show("Error: $e");
- }
- }
- void handlerResultList(List<RewardsListList>? list) {
- if (list != null && list.isNotEmpty) {
- //有数据,判断是刷新还是加载更多的数据
- if (page == 1) {
- //刷新的方式
- state.list!.clear();
- state.list!.addAll(list.map((item) => item.toJson()).toList());
- refreshController.finishRefresh();
- Log.d("state.liststate.liststate.list------${state.list}");
- //更新展示的状态
- changeLoadingState(LoadState.State_Success, null);
- } else {
- //加载更多
- final allList = state.list;
- state = state.copyWith(list: allList);
- refreshController.finishLoad();
- // update();
- }
- } else {
- if (page == 1) {
- //展示无数据的布局
- state.list!.clear();
- changeLoadingState(LoadState.State_Empty, null);
- refreshController.finishRefresh();
- } else {
- //展示加载完成,没有更多数据了
- if (state.list!.length == 0) {
- changeLoadingState(LoadState.State_Empty, null);
- } else {
- changeLoadingState(LoadState.State_Success, null);
- }
- refreshController.finishLoad(IndicatorResult.noMore);
- }
- }
- }
- }
|