Просмотр исходного кода

1、搜索结果页数据更新问题

tags/0.0.14+1^0
PH2 3 лет назад
Родитель
Сommit
d02c1af948
8 измененных файлов: 156 добавлений и 120 удалений
  1. +20
    -5
      lib/pages/search_result_page/item/bloc/search_result_item_bloc.dart
  2. +22
    -1
      lib/pages/search_result_page/item/bloc/search_result_item_event.dart
  3. +26
    -1
      lib/pages/search_result_page/item/bloc/search_result_item_state.dart
  4. +32
    -9
      lib/pages/search_result_page/item/search_result_item_page.dart
  5. +5
    -63
      lib/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart
  6. +2
    -1
      lib/widgets/search_result/goods_list/bloc/search_result_goods_list_repository.dart
  7. +18
    -0
      lib/widgets/search_result/goods_list/bloc/search_result_goods_list_state.dart
  8. +31
    -40
      lib/widgets/search_result/goods_list/search_result_goods_list_widget.dart

+ 20
- 5
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<SearchResultItemEvent, SearchResultItemState> {
// SearchResultItemBloc() : super(SearchResultItemInitial());
@@ -23,9 +21,26 @@ class SearchResultItemBloc extends Bloc<SearchResultItemEvent, SearchResultItemS
Stream<SearchResultItemState> 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();
}
}

/// 初始化


+ 22
- 1
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<Object> get props => [];
}

/// 初始化
@@ -13,3 +16,21 @@ class SearchResultItemInitEvent extends SearchResultItemEvent{
@override
List<Object> 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 {}

+ 26
- 1
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;
}

+ 32
- 9
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<String, dynamic> data;

@@ -96,6 +98,27 @@ class _SearchResultItemPageContianerState extends State<SearchResultItemPageCont
buildWhen: (previous, current) {
/// 数据加载出错不进行build
if (current is SearchResultItemErrorState) {
_refreshController?.loadNoData();
return false;
}
// 刷新成功
if (current is SearchResultItemRefreshSuccessState) {
_refreshController?.refreshCompleted(resetFooterState: true);
return false;
}
// 刷新失败
if (current is SearchResultItemRefreshErrorState) {
_refreshController?.refreshFailed();
return false;
}
// 下拉加载成功
if (current is SearchResultItemLoadSuccessState) {
_refreshController?.loadComplete();
return false;
}
// 下拉加载失败
if (current is SearchResultItemLoadNoMoreState) {
_refreshController?.loadNoData();
return false;
}
return true;
@@ -115,13 +138,13 @@ class _SearchResultItemPageContianerState extends State<SearchResultItemPageCont
/// 上拉更多
void _onLoading() async {
BlocProvider.of<SearchResultGoodsListBloc>(context).add(SearchResultGoodsListOnLoadEvent());
_refreshController.loadComplete();
// _refreshController.loadComplete();
}

/// 下拉刷新
void _onRefresh() async {
BlocProvider.of<SearchResultGoodsListBloc>(context).add(SearchResultGoodsListOnRefreshEvent());
_refreshController.refreshCompleted(resetFooterState: true);
// _refreshController.refreshCompleted(resetFooterState: true);
}

/// 获取主视图


+ 5
- 63
lib/widgets/search_result/goods_list/bloc/search_result_goods_list_bloc.dart Просмотреть файл

@@ -53,26 +53,6 @@ class SearchResultGoodsListBloc extends Bloc<SearchResultGoodsListEvent, SearchR
if(event is SearchResultGoodsListSortEvent){
yield* _mapSortEventToState(event);
}

// /// 综合
// if (event is SearchResultGoodsListZHSortEvent) {
// yield* _mapZhSortEventToState(event);
// }
//
// /// 补贴
// if (event is SearchResultGoodsListBtSortEvent) {
// yield* _mapBTSortEventToState(event);
// }
//
// /// 优惠券
// if (event is SearchResultGoodsListYHQSortEvent) {
// yield* _mapYHQSortEventToState(event);
// }
//
// /// 筛选
// if (event is SearchResultGoodsListSXSortEvent) {
// yield* _mapSXSortEventToState(event);
// }
}

/// 初始化事件
@@ -89,9 +69,10 @@ class SearchResultGoodsListBloc extends Bloc<SearchResultGoodsListEvent, SearchR
Stream<SearchResultGoodsListState> _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<SearchResultGoodsListEvent, SearchR
Stream<SearchResultGoodsListState> _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<SearchResultGoodsListEvent, SearchR
if (!EmptyUtil.isEmpty(result)) {
yield SearchResultGoodsListChangeStyleState(result['style'], result['datas']);
} else {
// yield SearchResultGoodsListErrorState();
// yield SearchResultGoodsListNoMoreDataState();
}
}

@@ -134,44 +116,4 @@ class SearchResultGoodsListBloc extends Bloc<SearchResultGoodsListEvent, SearchR
// yield SearchResultGoodsListErrorState();
}
}

// /// 综合排序
// Stream<SearchResultGoodsListState> _mapZhSortEventToState(SearchResultGoodsListZHSortEvent event) async* {
// var result = await repository.fetchZhSort(event.args);
// if (!EmptyUtil.isEmpty(result)) {
// yield SearchResultGoodsListLoadedState(model: result);
// } else {
// yield SearchResultGoodsListErrorState();
// }
// }
//
// /// 补贴排序
// Stream<SearchResultGoodsListState> _mapBTSortEventToState(SearchResultGoodsListBtSortEvent event) async* {
// var result = await repository.fetchBt(event.args);
// if (!EmptyUtil.isEmpty(result)) {
// yield SearchResultGoodsListLoadedState(model: result);
// } else {
// yield SearchResultGoodsListErrorState();
// }
// }
//
// /// 优惠券排序
// Stream<SearchResultGoodsListState> _mapYHQSortEventToState(SearchResultGoodsListYHQSortEvent event) async* {
// var result = await repository.fetchYhq(event.args);
// if (!EmptyUtil.isEmpty(result)) {
// yield SearchResultGoodsListLoadedState(model: result);
// } else {
// yield SearchResultGoodsListErrorState();
// }
// }
//
// /// 筛选排序
// Stream<SearchResultGoodsListState> _mapSXSortEventToState(SearchResultGoodsListSXSortEvent event) async* {
// var result = await repository.fetchSX(event.args);
// if (!EmptyUtil.isEmpty(result)) {
// yield SearchResultGoodsListLoadedState(model: result);
// } else {
// yield SearchResultGoodsListErrorState();
// }
// }
}

+ 2
- 1
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;
}



+ 18
- 0
lib/widgets/search_result/goods_list/bloc/search_result_goods_list_state.dart Просмотреть файл

@@ -43,3 +43,21 @@ class SearchResultGoodsListChangeStyleState extends SearchResultGoodsListState {
@override
List<Object> 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;
}

+ 31
- 40
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<String, dynamic> data;
HomeGoodsStyleModel style;
@@ -52,16 +55,6 @@ class _SearchResultGoodsListWidgetContainerState extends State<SearchResultGoods
/// 显示样式
bool _isShowOneColumn = true;

/// 下拉刷新
void _onRefersh() async {
BlocProvider.of<SearchResultGoodsListBloc>(context).add(SearchResultGoodsListOnRefreshEvent());
}

/// 上拉更多
void _onLoad() async {
BlocProvider.of<SearchResultGoodsListBloc>(context).add(SearchResultGoodsListOnLoadEvent());
}

@override
void initState() {
/// 初始化
@@ -71,36 +64,34 @@ class _SearchResultGoodsListWidgetContainerState extends State<SearchResultGoods

@override
Widget build(BuildContext context) {
// return BlocConsumer<SearchResultGoodsListBloc, SearchResultGoodsListState>(
// 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<SearchResultGoodsListBloc, SearchResultGoodsListState>(
buildWhen: (previous, current) {
/// 没有更多数据
if (current is SearchResultGoodsListNoMoreDataState) {
// 通知页面的刷新control修改
BlocProvider.of<SearchResultItemBloc>(context).add(SearchResultItemLoadNoMoreEvent());
return false;
}
/// 上拉更多数据成功
if (current is SearchResultGoodsListLoadSuccessState) {
// 通知页面的刷新control修改
BlocProvider.of<SearchResultItemBloc>(context).add(SearchResultItemLoadSuccessEvent());
return false;
}
/// 刷新数据成功
if (current is SearchResultGoodsListRefreshSuccessState) {
// 通知页面的刷新control修改
BlocProvider.of<SearchResultItemBloc>(context).add(SearchResultItemRefreshSuccessEvent());
return false;
}
/// 刷新数据失败
if (current is SearchResultGoodsListRefreshErrorState) {
// 通知页面的刷新control修改
BlocProvider.of<SearchResultItemBloc>(context).add(SearchResultItemRefreshErrorEvent());
return false;
}
return true;
},
builder: (context, state) {
if (state is SearchResultGoodsListLoadedState) {
return _getMainWidget(state?.model, widget?.style);


Загрузка…
Отмена
Сохранить