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

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