基础组件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_tab_widget.dart 6.6 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import 'dart:convert';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:cached_network_image/cached_network_image.dart';
  5. import 'package:tab_indicator_styler/tab_indicator_styler.dart';
  6. import 'package:zhiying_base_widget/pages/search_page/item/search_item_page.dart';
  7. import 'package:zhiying_base_widget/pages/search_page/notifier/search_tag_notifier.dart';
  8. import 'package:zhiying_base_widget/pages/search_result_page/search_result_page.dart';
  9. import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_bloc.dart';
  10. import 'package:zhiying_base_widget/pages/search_think_page/model/search_think_model.dart';
  11. import 'package:zhiying_base_widget/widgets/home/home_quick_entry/cached_network_image_util.dart';
  12. import 'package:zhiying_base_widget/widgets/search/tabbar/search_tab_sk.dart';
  13. import 'package:zhiying_base_widget/widgets/search/widget/my_tab.dart';
  14. import 'package:flutter_bloc/flutter_bloc.dart';
  15. import 'package:zhiying_comm/zhiying_comm.dart';
  16. import 'package:provider/provider.dart';
  17. import 'model/search_tab_model.dart';
  18. class SearchTabWidget extends StatefulWidget {
  19. final Map<String, dynamic> data;
  20. SearchTabModel model;
  21. SearchTabWidget(this.data, {Key key}) : super(key: key) {
  22. try {
  23. model = SearchTabModel.fromJson(jsonDecode(data['data']));
  24. } catch (e) {
  25. Logger.error(e.toString());
  26. }
  27. }
  28. @override
  29. _SearchTabWidgetState createState() => _SearchTabWidgetState();
  30. }
  31. class _SearchTabWidgetState extends State<SearchTabWidget> {
  32. TabController _tabController;
  33. String _type = GlobalConfig.PROVIDER_TB;
  34. /// 联想列表的item点击事件
  35. _onThinkItemClick(SearchThinkModel model){
  36. model.type = _type;
  37. RouterUtil.hideKeyboard(context);
  38. Provider.of<SearchTagNotifier>(context, listen: false).addTag(model?.keywords);
  39. BlocProvider.of<SearchThinkBloc>(context).add(SearchThinkShowBaseViewEvent());
  40. Navigator.push(context, CupertinoPageRoute(
  41. builder: (_)=> SearchResultPage(model.toJson()..['tag'] = 'search_page')
  42. ));
  43. }
  44. @override
  45. void initState() {
  46. _tabController = TabController(length: widget?.model?.search_icon_list?.length ?? 0, vsync: ScrollableState())..addListener((){
  47. // String type = '';
  48. // try{
  49. // type = widget.model.search_icon_list[_tabController.index].type;
  50. // }catch(_){}
  51. // if(!EmptyUtil.isEmpty(type)) {
  52. // BlocProvider.of<SearchThinkBloc>(context).add(SearchThinkChangeTypeEvent(type));
  53. // }
  54. if(!_tabController.indexIsChanging){
  55. Logger.log('_tabController.indexIsChanging = ${_tabController?.index}');
  56. try{
  57. _type = widget.model.search_icon_list[_tabController.index].type;
  58. Provider.of<SearchTagNotifier>(context, listen: false).setType(_type);
  59. }catch(e, s){
  60. Logger.error(e,s);
  61. }
  62. }
  63. });
  64. super.initState();
  65. }
  66. @override
  67. void dispose() {
  68. _tabController?.dispose();
  69. super.dispose();
  70. }
  71. @override
  72. Widget build(BuildContext context) {
  73. return _getMainWidget(widget?.model);
  74. }
  75. /// 获取主视图
  76. Widget _getMainWidget(SearchTabModel model) {
  77. return Visibility(
  78. replacement: SearchTabSkeleton(),
  79. visible: !EmptyUtil.isEmpty(model),
  80. child: _getTabar(model),
  81. );
  82. }
  83. /// 获取TabBar
  84. Widget _getTabar(SearchTabModel model) {
  85. return Container(
  86. margin: const EdgeInsets.only(/*left: 12.5, right: 12.5,*/ top: 20),
  87. child: Column(
  88. children: <Widget>[
  89. TabBar(
  90. controller: _tabController,
  91. isScrollable: true,
  92. labelStyle: TextStyle( fontSize: 14, fontWeight: FontWeight.bold),
  93. unselectedLabelColor: HexColor.fromHex( model?.nameColor ?? '#999999'),
  94. labelColor: HexColor.fromHex(model?.nameSelectColor ?? '#FF4242'),
  95. // indicatorSize: TabBarIndicatorSize.label,
  96. indicator: MaterialIndicator(
  97. height: 2.5,
  98. topLeftRadius: 8,
  99. topRightRadius: 8,
  100. bottomLeftRadius: 8,
  101. bottomRightRadius: 8,
  102. color: HexColor.fromHex(model?.lineSelectColor ?? '#FF4242'),
  103. horizontalPadding: 25,
  104. ),
  105. tabs: model.search_icon_list.map((item) {
  106. return MyTab(
  107. icon: CachedNetworkImage(imageUrl: item?.with_icon_color ?? '', width: 14,),
  108. text: item.name,
  109. );
  110. }).toList(),
  111. ),
  112. Expanded(
  113. child: _getItemWidget(model),
  114. ),
  115. ],
  116. ),
  117. );
  118. }
  119. /// 根据输入框,是否显示联想列表还是其它
  120. Widget _getItemWidget(SearchTabModel model){
  121. return Stack(
  122. children: <Widget>[
  123. _getTabBarView(model),
  124. BlocConsumer<SearchThinkBloc, SearchThinkState>(
  125. listener: (context, state){},
  126. buildWhen: (prev, current){
  127. if(current is SearchThinkErrorState){
  128. return false;
  129. }
  130. return true;
  131. },
  132. builder: (context, state){
  133. // return Visibility(
  134. // replacement: _getTabBarView(model),
  135. //
  136. // child: _getThinkListWidget(),
  137. // );
  138. if(state is SearchThinkLoadedState){
  139. return _getThinkListWidget(state.model);
  140. }
  141. return Container();
  142. },
  143. ),
  144. ],
  145. );
  146. }
  147. /// tabBarView
  148. Widget _getTabBarView(SearchTabModel model){
  149. return TabBarView(
  150. controller: _tabController,
  151. children: model.search_icon_list.map((item){
  152. // TODO 这里需要和后台沟通改成页面的唯一标示
  153. // return PageFactory.create('search_item_page', item.toJson());
  154. return SearchItemPage(item.toJson());
  155. }).toList(),
  156. );
  157. }
  158. /// 联想列表
  159. Widget _getThinkListWidget(List<SearchThinkModel> model){
  160. return Container(
  161. color: Colors.white,
  162. height: double.infinity,
  163. child: ListView.builder(itemBuilder: (context, index){
  164. SearchThinkModel item = model[index];
  165. return GestureDetector(
  166. onTap: ()=> _onThinkItemClick(item),
  167. child: Container(
  168. decoration: BoxDecoration(
  169. border: Border(bottom: BorderSide(width: 0.5, color: HexColor.fromHex('#EEEEEE')))
  170. ),
  171. padding: const EdgeInsets.only(top: 13, bottom: 13),
  172. child: Text('${item?.keywords}', style: TextStyle( color: HexColor.fromHex('#333333'), fontSize: 14),),
  173. ),
  174. );
  175. },
  176. itemCount: model?.length ?? 0,
  177. padding: const EdgeInsets.only(left: 12.5, right: 12.5),
  178. shrinkWrap: true,
  179. ),
  180. );
  181. }
  182. }