From d02c1af948afb7fe69ca42d42baad75321c83437 Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Fri, 16 Apr 2021 18:03:13 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E9=A1=B5=E6=95=B0=E6=8D=AE=E6=9B=B4=E6=96=B0=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../item/bloc/search_result_item_bloc.dart | 25 +++++-- .../item/bloc/search_result_item_event.dart | 23 +++++- .../item/bloc/search_result_item_state.dart | 27 ++++++- .../item/search_result_item_page.dart | 41 ++++++++--- .../bloc/search_result_goods_list_bloc.dart | 68 ++---------------- .../search_result_goods_list_repository.dart | 3 +- .../bloc/search_result_goods_list_state.dart | 18 +++++ .../search_result_goods_list_widget.dart | 71 ++++++++----------- 8 files changed, 156 insertions(+), 120 deletions(-) diff --git a/lib/pages/search_result_page/item/bloc/search_result_item_bloc.dart b/lib/pages/search_result_page/item/bloc/search_result_item_bloc.dart index f4fe1e3..04d2c8f 100644 --- a/lib/pages/search_result_page/item/bloc/search_result_item_bloc.dart +++ b/lib/pages/search_result_page/item/bloc/search_result_item_bloc.dart @@ -1,14 +1,12 @@ import 'dart:async'; import 'package:bloc/bloc.dart'; -import 'package:equatable/equatable.dart'; + import 'package:flutter/cupertino.dart'; import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_repository.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; - -part 'search_result_item_event.dart'; - -part 'search_result_item_state.dart'; +import 'search_result_item_state.dart'; +import 'search_result_item_event.dart'; class SearchResultItemBloc extends Bloc { // SearchResultItemBloc() : super(SearchResultItemInitial()); @@ -23,9 +21,26 @@ class SearchResultItemBloc extends Bloc mapEventToState( SearchResultItemEvent event, ) async* { + /// 初始化 if (event is SearchResultItemInitEvent) { yield* _mapInitEventToState(event); } + /// 下拉刷新成功事件 + if(event is SearchResultItemRefreshSuccessEvent) { + yield SearchResultItemRefreshSuccessState(); + } + /// 下拉刷新失败事件 + if(event is SearchResultItemRefreshErrorEvent) { + yield SearchResultItemRefreshErrorState(); + } + /// 上拉更多成功事件 + if(event is SearchResultItemLoadSuccessEvent) { + yield SearchResultItemLoadSuccessState(); + } + /// 上拉更多失败事件 + if(event is SearchResultItemLoadNoMoreEvent) { + yield SearchResultItemLoadNoMoreState(); + } } /// 初始化 diff --git a/lib/pages/search_result_page/item/bloc/search_result_item_event.dart b/lib/pages/search_result_page/item/bloc/search_result_item_event.dart index 16fb696..4052141 100644 --- a/lib/pages/search_result_page/item/bloc/search_result_item_event.dart +++ b/lib/pages/search_result_page/item/bloc/search_result_item_event.dart @@ -1,7 +1,10 @@ -part of 'search_result_item_bloc.dart'; +import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; abstract class SearchResultItemEvent extends Equatable { const SearchResultItemEvent(); + @override + List get props => []; } /// 初始化 @@ -13,3 +16,21 @@ class SearchResultItemInitEvent extends SearchResultItemEvent{ @override List get props => [this.model]; } + +/// 下拉刷新成功事件 +class SearchResultItemRefreshSuccessEvent extends SearchResultItemEvent { + @override + bool operator == (Object other) => false; +} +/// 下拉刷新失败事件 +class SearchResultItemRefreshErrorEvent extends SearchResultItemEvent { + @override + bool operator == (Object other) => false; +} +/// 上拉更多成功事件 +class SearchResultItemLoadSuccessEvent extends SearchResultItemEvent { + @override + bool operator == (Object other) => false; +} +/// 上拉更多失败事件 +class SearchResultItemLoadNoMoreEvent extends SearchResultItemEvent {} \ No newline at end of file diff --git a/lib/pages/search_result_page/item/bloc/search_result_item_state.dart b/lib/pages/search_result_page/item/bloc/search_result_item_state.dart index 55ec28e..955c7fe 100644 --- a/lib/pages/search_result_page/item/bloc/search_result_item_state.dart +++ b/lib/pages/search_result_page/item/bloc/search_result_item_state.dart @@ -1,4 +1,5 @@ -part of 'search_result_item_bloc.dart'; +import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; abstract class SearchResultItemState extends Equatable { const SearchResultItemState(); @@ -21,3 +22,27 @@ class SearchResultItemLoadedState extends SearchResultItemState { /// 数据加载失败 class SearchResultItemErrorState extends SearchResultItemState {} + +/// 刷新成功 +class SearchResultItemRefreshSuccessState extends SearchResultItemState { + @override + bool operator == (Object other) => false; +} + +/// 刷新失败 +class SearchResultItemRefreshErrorState extends SearchResultItemState { + @override + bool operator == (Object other) => false; +} + +/// 下拉失败 +class SearchResultItemLoadNoMoreState extends SearchResultItemState { + @override + bool operator == (Object other) => false; +} + +/// 下拉成功 +class SearchResultItemLoadSuccessState extends SearchResultItemState { + @override + bool operator == (Object other) => false; +} \ No newline at end of file diff --git a/lib/pages/search_result_page/item/search_result_item_page.dart b/lib/pages/search_result_page/item/search_result_item_page.dart index 703c5ab..cbf4cc4 100644 --- a/lib/pages/search_result_page/item/search_result_item_page.dart +++ b/lib/pages/search_result_page/item/search_result_item_page.dart @@ -1,18 +1,20 @@ import 'package:flutter/material.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; -import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_bloc.dart'; -import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_repository.dart'; import 'package:zhiying_base_widget/widgets/home/home_goods/skeleton/home_goods_sk.dart'; -import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_gif_header.dart'; -import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_header.dart'; -import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart'; -import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:provider/provider.dart'; - +import 'bloc/search_result_item_bloc.dart'; import 'notifier/search_result_item_page_notifier.dart'; +import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_bloc.dart'; +import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_repository.dart'; +import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_state.dart'; +import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_event.dart'; + +import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart'; +import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart'; + class SearchResultItemPage extends StatelessWidget { final Map data; @@ -96,6 +98,27 @@ class _SearchResultItemPageContianerState extends State(context).add(SearchResultGoodsListOnLoadEvent()); - _refreshController.loadComplete(); + // _refreshController.loadComplete(); } /// 下拉刷新 void _onRefresh() async { BlocProvider.of(context).add(SearchResultGoodsListOnRefreshEvent()); - _refreshController.refreshCompleted(resetFooterState: true); + // _refreshController.refreshCompleted(resetFooterState: true); } /// 获取主视图 diff --git a/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart b/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart index d7632ae..9430d19 100644 --- a/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart +++ b/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart @@ -53,26 +53,6 @@ class SearchResultGoodsListBloc extends Bloc _mapRefreshToState(SearchResultGoodsListOnRefreshEvent event) async* { var result = await repository.fetchRefreshData(); if (!EmptyUtil.isEmpty(result)) { + yield SearchResultGoodsListRefreshSuccessState(); yield SearchResultGoodsListLoadedState(model: result); } else { - // yield SearchResultGoodsListErrorState(); + yield SearchResultGoodsListRefreshErrorState(); } } @@ -99,9 +80,10 @@ class SearchResultGoodsListBloc extends Bloc _mapOnLoadToState(SearchResultGoodsListOnLoadEvent event, SearchResultGoodsListLoadedState state) async* { var result = await repository.fetchLoadData(); if (!EmptyUtil.isEmpty(result)) { + yield SearchResultGoodsListLoadSuccessState(); yield SearchResultGoodsListLoadedState(model: result); } else { - // yield SearchResultGoodsListErrorState(); + yield SearchResultGoodsListNoMoreDataState(); } } @@ -111,7 +93,7 @@ class SearchResultGoodsListBloc extends Bloc _mapZhSortEventToState(SearchResultGoodsListZHSortEvent event) async* { - // var result = await repository.fetchZhSort(event.args); - // if (!EmptyUtil.isEmpty(result)) { - // yield SearchResultGoodsListLoadedState(model: result); - // } else { - // yield SearchResultGoodsListErrorState(); - // } - // } - // - // /// 补贴排序 - // Stream _mapBTSortEventToState(SearchResultGoodsListBtSortEvent event) async* { - // var result = await repository.fetchBt(event.args); - // if (!EmptyUtil.isEmpty(result)) { - // yield SearchResultGoodsListLoadedState(model: result); - // } else { - // yield SearchResultGoodsListErrorState(); - // } - // } - // - // /// 优惠券排序 - // Stream _mapYHQSortEventToState(SearchResultGoodsListYHQSortEvent event) async* { - // var result = await repository.fetchYhq(event.args); - // if (!EmptyUtil.isEmpty(result)) { - // yield SearchResultGoodsListLoadedState(model: result); - // } else { - // yield SearchResultGoodsListErrorState(); - // } - // } - // - // /// 筛选排序 - // Stream _mapSXSortEventToState(SearchResultGoodsListSXSortEvent event) async* { - // var result = await repository.fetchSX(event.args); - // if (!EmptyUtil.isEmpty(result)) { - // yield SearchResultGoodsListLoadedState(model: result); - // } else { - // yield SearchResultGoodsListErrorState(); - // } - // } } diff --git a/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart b/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart index 2be40de..24142d0 100644 --- a/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart +++ b/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart @@ -99,6 +99,8 @@ class SearchResultGoodsListRepository { if (data.length >= MAX) { _hasNomore = true; ++_currentPage; + } else { + _hasNomore = false; } // 下拉刷新需要清理 if (refresh) { @@ -117,7 +119,6 @@ class SearchResultGoodsListRepository { } catch (e) { Logger.error(e); } - _hasNomore = false; return null; } diff --git a/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_state.dart b/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_state.dart index dc71776..1879a26 100644 --- a/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_state.dart +++ b/lib/widgets/search_result/goods_list/bloc/search_result_goods_list_state.dart @@ -43,3 +43,21 @@ class SearchResultGoodsListChangeStyleState extends SearchResultGoodsListState { @override List get props => [this.isShowOneColumn]; } + +/// 没有更多数据 +class SearchResultGoodsListNoMoreDataState extends SearchResultGoodsListState { + @override + bool operator == (Object other) => false; +} + +/// 上拉数据成功 +class SearchResultGoodsListLoadSuccessState extends SearchResultGoodsListState {} + +/// 刷新数据成功 +class SearchResultGoodsListRefreshSuccessState extends SearchResultGoodsListState {} + +/// 刷新数据失败 +class SearchResultGoodsListRefreshErrorState extends SearchResultGoodsListState { + @override + bool operator == (Object other) => false; +} diff --git a/lib/widgets/search_result/goods_list/search_result_goods_list_widget.dart b/lib/widgets/search_result/goods_list/search_result_goods_list_widget.dart index 7714334..059873d 100644 --- a/lib/widgets/search_result/goods_list/search_result_goods_list_widget.dart +++ b/lib/widgets/search_result/goods_list/search_result_goods_list_widget.dart @@ -1,7 +1,6 @@ import 'dart:convert'; import 'package:flutter/material.dart'; -import 'package:zhiying_base_widget/pages/goods_details_page/goods_details_page.dart'; import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart'; import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item.dart'; import 'package:zhiying_base_widget/widgets/home/home_goods/home_goods_item_single.dart'; @@ -12,6 +11,10 @@ import 'package:zhiying_base_widget/widgets/search_result/goods_list/bloc/search import 'package:zhiying_comm/zhiying_comm.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_bloc.dart'; +import 'package:zhiying_base_widget/pages/search_result_page/item/bloc/search_result_item_event.dart'; + + class SearchResultGoodsListWidget extends StatefulWidget { final Map data; HomeGoodsStyleModel style; @@ -52,16 +55,6 @@ class _SearchResultGoodsListWidgetContainerState extends State(context).add(SearchResultGoodsListOnRefreshEvent()); - } - - /// 上拉更多 - void _onLoad() async { - BlocProvider.of(context).add(SearchResultGoodsListOnLoadEvent()); - } - @override void initState() { /// 初始化 @@ -71,36 +64,34 @@ class _SearchResultGoodsListWidgetContainerState extends State( - // listener: (BuildContext context, SearchResultGoodsListState state) { - // if (state is SearchResultGoodsListErrorState) { - // print('search_result_goods_list_widget = 数据加载出错'); - // } - // }, - // buildWhen: (previous, current) { - // /// 数据加载出错不进行build - // if (current is SearchResultGoodsListErrorState) { - // return false; - // } - // - // return true; - // }, - // builder: (context, state) { - // if (state is SearchResultGoodsListLoadedState) { - // return _getMainWidget(state?.model, widget?.style); - // } - // if (state is SearchResultGoodsListChangeStyleState) { - // _isShowOneColumn = state.isShowOneColumn; - // return _getMainWidget(state?.model, widget?.style); - // } - // - // /// 骨架屏幕 - // return HomeGoodsSkeleton(); - // }, - // ); - - return BlocBuilder( + buildWhen: (previous, current) { + /// 没有更多数据 + if (current is SearchResultGoodsListNoMoreDataState) { + // 通知页面的刷新control修改 + BlocProvider.of(context).add(SearchResultItemLoadNoMoreEvent()); + return false; + } + /// 上拉更多数据成功 + if (current is SearchResultGoodsListLoadSuccessState) { + // 通知页面的刷新control修改 + BlocProvider.of(context).add(SearchResultItemLoadSuccessEvent()); + return false; + } + /// 刷新数据成功 + if (current is SearchResultGoodsListRefreshSuccessState) { + // 通知页面的刷新control修改 + BlocProvider.of(context).add(SearchResultItemRefreshSuccessEvent()); + return false; + } + /// 刷新数据失败 + if (current is SearchResultGoodsListRefreshErrorState) { + // 通知页面的刷新control修改 + BlocProvider.of(context).add(SearchResultItemRefreshErrorEvent()); + return false; + } + return true; + }, builder: (context, state) { if (state is SearchResultGoodsListLoadedState) { return _getMainWidget(state?.model, widget?.style);