基础组件库
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.5 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. }catch(e, s){
  59. Logger.error(e,s);
  60. }
  61. }
  62. });
  63. super.initState();
  64. }
  65. @override
  66. void dispose() {
  67. _tabController?.dispose();
  68. super.dispose();
  69. }
  70. @override
  71. Widget build(BuildContext context) {
  72. return _getMainWidget(widget?.model);
  73. }
  74. /// 获取主视图
  75. Widget _getMainWidget(SearchTabModel model) {
  76. return Visibility(
  77. replacement: SearchTabSkeleton(),
  78. visible: !EmptyUtil.isEmpty(model),
  79. child: _getTabar(model),
  80. );
  81. }
  82. /// 获取TabBar
  83. Widget _getTabar(SearchTabModel model) {
  84. return Container(
  85. margin: const EdgeInsets.only(/*left: 12.5, right: 12.5,*/ top: 20),
  86. child: Column(
  87. children: <Widget>[
  88. TabBar(
  89. controller: _tabController,
  90. isScrollable: true,
  91. labelStyle: TextStyle( fontSize: 14, fontWeight: FontWeight.bold),
  92. unselectedLabelColor: HexColor.fromHex( model?.nameColor ?? '#999999'),
  93. labelColor: HexColor.fromHex(model?.nameSelectColor ?? '#FF4242'),
  94. // indicatorSize: TabBarIndicatorSize.label,
  95. indicator: MaterialIndicator(
  96. height: 2.5,
  97. topLeftRadius: 8,
  98. topRightRadius: 8,
  99. bottomLeftRadius: 8,
  100. bottomRightRadius: 8,
  101. color: HexColor.fromHex(model?.lineSelectColor ?? '#FF4242'),
  102. horizontalPadding: 25,
  103. ),
  104. tabs: model.search_icon_list.map((item) {
  105. return MyTab(
  106. icon: CachedNetworkImage(imageUrl: item?.with_icon_color ?? '', width: 14,),
  107. text: item.name,
  108. );
  109. }).toList(),
  110. ),
  111. Expanded(
  112. child: _getItemWidget(model),
  113. ),
  114. ],
  115. ),
  116. );
  117. }
  118. /// 根据输入框,是否显示联想列表还是其它
  119. Widget _getItemWidget(SearchTabModel model){
  120. return Stack(
  121. children: <Widget>[
  122. _getTabBarView(model),
  123. BlocConsumer<SearchThinkBloc, SearchThinkState>(
  124. listener: (context, state){},
  125. buildWhen: (prev, current){
  126. if(current is SearchThinkErrorState){
  127. return false;
  128. }
  129. return true;
  130. },
  131. builder: (context, state){
  132. // return Visibility(
  133. // replacement: _getTabBarView(model),
  134. //
  135. // child: _getThinkListWidget(),
  136. // );
  137. if(state is SearchThinkLoadedState){
  138. return _getThinkListWidget(state.model);
  139. }
  140. return Container();
  141. },
  142. ),
  143. ],
  144. );
  145. }
  146. /// tabBarView
  147. Widget _getTabBarView(SearchTabModel model){
  148. return TabBarView(
  149. controller: _tabController,
  150. children: model.search_icon_list.map((item){
  151. // TODO 这里需要和后台沟通改成页面的唯一标示
  152. // return PageFactory.create('search_item_page', item.toJson());
  153. return SearchItemPage(item.toJson());
  154. }).toList(),
  155. );
  156. }
  157. /// 联想列表
  158. Widget _getThinkListWidget(List<SearchThinkModel> model){
  159. return Container(
  160. color: Colors.white,
  161. height: double.infinity,
  162. child: ListView.builder(itemBuilder: (context, index){
  163. SearchThinkModel item = model[index];
  164. return GestureDetector(
  165. onTap: ()=> _onThinkItemClick(item),
  166. child: Container(
  167. decoration: BoxDecoration(
  168. border: Border(bottom: BorderSide(width: 0.5, color: HexColor.fromHex('#EEEEEE')))
  169. ),
  170. padding: const EdgeInsets.only(top: 13, bottom: 13),
  171. child: Text('${item?.keywords}', style: TextStyle( color: HexColor.fromHex('#333333'), fontSize: 14),),
  172. ),
  173. );
  174. },
  175. itemCount: model?.length ?? 0,
  176. padding: const EdgeInsets.only(left: 12.5, right: 12.5),
  177. shrinkWrap: true,
  178. ),
  179. );
  180. }
  181. }