From 12cffd46cb07618747283af40bea5f241375c058 Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Thu, 22 Oct 2020 09:50:17 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BA=BA=E8=84=89=E7=9A=84=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=9A=84=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notifier/team_page_notifier.dart | 25 ++++++++++++++++--- lib/pages/team_page/team_page.dart | 2 +- .../fans_list/bloc/team_list_fans_bloc.dart | 17 +++++++++++++ .../fans_list/bloc/team_list_fans_event.dart | 8 ++++++ .../bloc/team_list_fans_repository.dart | 24 +++++++++++++++--- .../team/fans_list/team_fans_widget.dart | 18 +++++++++---- .../team/filter/team_filter_widget.dart | 20 +++++++++++++++ lib/widgets/team/input/team_input_widget.dart | 24 ++++++++++++------ 8 files changed, 119 insertions(+), 19 deletions(-) diff --git a/lib/pages/team_page/notifier/team_page_notifier.dart b/lib/pages/team_page/notifier/team_page_notifier.dart index f0f131b..75d5108 100644 --- a/lib/pages/team_page/notifier/team_page_notifier.dart +++ b/lib/pages/team_page/notifier/team_page_notifier.dart @@ -4,20 +4,30 @@ import 'package:zhiying_comm/util/log/let_log.dart'; class TeamPageNotifier with ChangeNotifier { /// taBar选中的index int tabSelectIndex = 0; + bool isSearch = false; + String keyword; /// 筛选的条件 Map reqArgs = {}; /// 更新筛选条件 void updateSortCondition(String key, String args) { - reqArgs[key] = args; - Logger.log('updateSortCondition reqArgs = ${reqArgs?.toString()}'); + this.isSearch = false; + this.reqArgs[key] = args; + notifyListeners(); + } + + /// 重置 + void searchReset(String keyword) { + this.isSearch = true; + this.keyword = keyword; + this.reqArgs.clear(); notifyListeners(); } /// 更新选中Index void updateTabIndex(int index) { - print('111111'); + isSearch = false; this.tabSelectIndex = index; } @@ -30,4 +40,13 @@ class TeamPageNotifier with ChangeNotifier { int getTabIndex() { return this.tabSelectIndex; } + + /// 获取搜索关键字 + String getKeyword(){ + return this.keyword; + } + /// 获取是否是搜索方式 + bool getSearch() { + return this.isSearch; + } } diff --git a/lib/pages/team_page/team_page.dart b/lib/pages/team_page/team_page.dart index cf85ef4..c8566d6 100644 --- a/lib/pages/team_page/team_page.dart +++ b/lib/pages/team_page/team_page.dart @@ -127,7 +127,7 @@ class _TeamPageContainerState extends State<_TeamPageContainer> { SliverToBoxAdapter(child: SizedBox(height: 8)), /// 输入框 - SliverPersistentHeader(delegate: CustomSliverPersistentHeaderDelegate(min: 34, max: 34, child: TeamInputWidget(styleModel)), pinned: true), + SliverPersistentHeader(delegate: CustomSliverPersistentHeaderDelegate(min: 34, max: 34, child: TeamInputWidget(styleModel, _controller)), pinned: true), /// 悬停TabBar SliverPersistentHeader( diff --git a/lib/widgets/team/fans_list/bloc/team_list_fans_bloc.dart b/lib/widgets/team/fans_list/bloc/team_list_fans_bloc.dart index b35773a..62f20c1 100644 --- a/lib/widgets/team/fans_list/bloc/team_list_fans_bloc.dart +++ b/lib/widgets/team/fans_list/bloc/team_list_fans_bloc.dart @@ -50,6 +50,12 @@ class TeamListFansBloc extends Bloc { if (event is TeamListFansFilterEvent) { yield* _mapFilterEventToState(event); } + + /// 搜索 + if(event is TeamListFansSearchEvent){ + yield* _mapSearchEventToState(event); + } + } /// 初始化 @@ -103,4 +109,15 @@ class TeamListFansBloc extends Bloc { yield TeamListFansErrorState(); } } + + /// 搜索 + Stream _mapSearchEventToState(TeamListFansSearchEvent event) async*{ + var result = await repository.fetchSearchData(event?.keyword); + if (!EmptyUtil.isEmpty(result)) { + yield TeamListFansLoadedState(model: result); + } else { + yield TeamListFansErrorState(); + } + } + } diff --git a/lib/widgets/team/fans_list/bloc/team_list_fans_event.dart b/lib/widgets/team/fans_list/bloc/team_list_fans_event.dart index d9b0cbc..c45967d 100644 --- a/lib/widgets/team/fans_list/bloc/team_list_fans_event.dart +++ b/lib/widgets/team/fans_list/bloc/team_list_fans_event.dart @@ -40,4 +40,12 @@ class TeamListFansFilterEvent extends TeamListFansEvent{ const TeamListFansFilterEvent({@required this.args}); @override List get props => [this.args]; +} + +/// 搜索 +class TeamListFansSearchEvent extends TeamListFansEvent{ + final String keyword; + TeamListFansSearchEvent(this.keyword); + @override + List get props => [this.keyword]; } \ No newline at end of file diff --git a/lib/widgets/team/fans_list/bloc/team_list_fans_repository.dart b/lib/widgets/team/fans_list/bloc/team_list_fans_repository.dart index abd56de..8acf47c 100644 --- a/lib/widgets/team/fans_list/bloc/team_list_fans_repository.dart +++ b/lib/widgets/team/fans_list/bloc/team_list_fans_repository.dart @@ -16,6 +16,24 @@ class TeamListFansRepository { TeamListFansRepository({this.type = 'all'}); + /// 搜索对方法 + Future fetchSearchData(String keyword) async { + try { + if (!EmptyUtil.isEmpty(keyword)) { + var result = await NetUtil.post('/api/v1/user/fans/search', params: {'keyword': keyword}, method: NetMethod.POST); + if (NetUtil.isSuccess(result) && !EmptyUtil.isEmpty(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA])) { + TeamFansListModel model = TeamFansListModel.fromJson(result[GlobalConfig.HTTP_RESPONSE_KEY_DATA]); + if (null != model && !EmptyUtil.isEmpty(model?.fans) && model.fans.length > 0) { + return model; + } + } + } + } catch (e, s) { + Logger.error(e, s); + } + return null; + } + /// 初始化方法 Future fetchInitData() async { _currentPage = 1; @@ -49,8 +67,8 @@ class TeamListFansRepository { } /// 整体筛选排序的方法 - Future fetchFilter(final Map reqArgs) async{ - if(!EmptyUtil.isEmpty(reqArgs)) { + Future fetchFilter(final Map reqArgs) async { + if (!EmptyUtil.isEmpty(reqArgs)) { _reqArgs = reqArgs; return fetchInitData(); } @@ -102,7 +120,7 @@ class TeamListFansRepository { _reqArgs.forEach((key, value) { // sb.write(key); // sb.write('='); - if(!EmptyUtil.isEmpty(value)) { + if (!EmptyUtil.isEmpty(value)) { sb.write(value); sb.write('&'); } diff --git a/lib/widgets/team/fans_list/team_fans_widget.dart b/lib/widgets/team/fans_list/team_fans_widget.dart index e01d882..9470d22 100644 --- a/lib/widgets/team/fans_list/team_fans_widget.dart +++ b/lib/widgets/team/fans_list/team_fans_widget.dart @@ -78,11 +78,19 @@ class __TeamFansWidgetContainerState extends State<_TeamFansWidgetContainer> { TeamPageNotifier notifier = Provider.of(context); if (null != notifier) { tabSelectIndex = notifier?.getTabIndex() ?? 0; - Map reqArgs = notifier?.getReqArgs() ?? null; - if (widget.tabCurrentIndex == tabSelectIndex && !EmptyUtil.isEmpty(reqArgs)) { - Logger.log('didChangeDependencies, currentTabIndex = ${widget?.tabCurrentIndex}, tabSelectIndex = $tabSelectIndex , reqArgs = ${reqArgs?.toString()}'); - // _refreshController.refreshToIdle(); - BlocProvider.of(context).add(TeamListFansFilterEvent(args: reqArgs)); + bool isSearch = notifier?.getSearch() ?? false; + Logger.log('isSearch ==== $isSearch'); + + /// 如果是搜索框 + if(isSearch){ + BlocProvider.of(context).add(TeamListFansSearchEvent(notifier?.getKeyword())); + }else { + Map reqArgs = notifier?.getReqArgs() ?? null; + if (widget.tabCurrentIndex == tabSelectIndex && !EmptyUtil.isEmpty(reqArgs)) { + Logger.log('didChangeDependencies, currentTabIndex = ${widget?.tabCurrentIndex}, tabSelectIndex = $tabSelectIndex , reqArgs = ${reqArgs?.toString()}'); + // _refreshController.refreshToIdle(); + BlocProvider.of(context).add(TeamListFansFilterEvent(args: reqArgs)); + } } } super.didChangeDependencies(); diff --git a/lib/widgets/team/filter/team_filter_widget.dart b/lib/widgets/team/filter/team_filter_widget.dart index edf419d..f3a1e53 100644 --- a/lib/widgets/team/filter/team_filter_widget.dart +++ b/lib/widgets/team/filter/team_filter_widget.dart @@ -43,6 +43,26 @@ class _TeamFilterWidgetState extends State { setState(() {}); } + @override + void didChangeDependencies() { + super.didChangeDependencies(); + try { + TeamPageNotifier notifier = Provider.of(context); + if (null != notifier) { + bool needRebuild = notifier?.getSearch()?? false; + if(needRebuild){ + widget.styleModel.teamViewSortList.forEach((element) { + element.selectState = 0; + }); + setState(() {}); + } + } + }catch(e, s){ + Logger.error(e, s); + } + + } + @override Widget build(BuildContext context) { return Container( diff --git a/lib/widgets/team/input/team_input_widget.dart b/lib/widgets/team/input/team_input_widget.dart index cfb87a1..3f539b2 100644 --- a/lib/widgets/team/input/team_input_widget.dart +++ b/lib/widgets/team/input/team_input_widget.dart @@ -1,12 +1,15 @@ import 'package:flutter/material.dart'; import 'package:zhiying_base_widget/pages/team_page/model/team_style_model.dart'; import 'package:cached_network_image/cached_network_image.dart'; +import 'package:zhiying_base_widget/pages/team_page/notifier/team_page_notifier.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; +import 'package:provider/provider.dart'; class TeamInputWidget extends StatefulWidget { TeamStyleModel styleModel; + TabController tabController; - TeamInputWidget(this.styleModel); + TeamInputWidget(this.styleModel, this.tabController); @override _TeamInputWidgetState createState() => _TeamInputWidgetState(); @@ -18,22 +21,28 @@ class _TeamInputWidgetState extends State { bool _showCancel = false; /// 搜索方法 - void _onSearchClick() {} + void _onSearchClick() { + String text = _controller?.text?.toString()?.trim(); + if (!EmptyUtil.isEmpty(text) && text.length > 0) { + widget?.tabController?.index = 0; + Provider.of(context, listen: false).searchReset(text); + } + } /// 点击取消输入 void _cancel() { _controller?.clear(); } - void _onChange(text){ - if(!EmptyUtil.isEmpty(text)){ - if(!_showCancel){ + void _onChange(text) { + if (!EmptyUtil.isEmpty(text)) { + if (!_showCancel) { setState(() { _showCancel = true; }); } - }else{ - if(_showCancel) { + } else { + if (_showCancel) { setState(() { _showCancel = false; }); @@ -105,6 +114,7 @@ class _TeamInputWidgetState extends State { controller: _controller, focusNode: _focusNode, onChanged: _onChange, + onSubmitted: (text) => _onSearchClick(), style: TextStyle(fontSize: 11, color: HexColor.fromHex(widget?.styleModel?.headerNoReferrerInputColor ?? '#000000'), textBaseline: TextBaseline.alphabetic), decoration: InputDecoration( focusedBorder: InputBorder.none,