基础组件库
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_page.dart 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import 'dart:convert';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:zhiying_base_widget/pages/search_page/notifier/search_tag_notifier.dart';
  5. import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_bloc.dart';
  6. import 'package:zhiying_base_widget/pages/search_think_page/bloc/search_think_repository.dart';
  7. import 'package:zhiying_comm/zhiying_comm.dart';
  8. import 'package:flutter_bloc/flutter_bloc.dart';
  9. import 'package:provider/provider.dart';
  10. import 'bloc/search_bloc.dart';
  11. import 'bloc/search_repository.dart';
  12. ///
  13. /// 搜索页
  14. ///
  15. class SearchPage extends StatelessWidget {
  16. final Map<String, dynamic> data;
  17. SearchPage(this.data, {Key key}) : super(key: key);
  18. @override
  19. Widget build(BuildContext context) {
  20. return MultiProvider(
  21. providers: [ChangeNotifierProvider.value(value: SearchTagNotifier())],
  22. child: MultiProvider(
  23. providers: [
  24. /// 搜索页面的bloc
  25. BlocProvider<SearchBloc>(
  26. create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)),
  27. ),
  28. /// 输入框联想的bloc
  29. BlocProvider<SearchThinkBloc>(
  30. create: (_) => SearchThinkBloc(SearchThinkRepository()),
  31. ),
  32. ],
  33. child: SearchPageContianer(
  34. data: data,
  35. ),
  36. ),
  37. // child: BlocProvider<SearchBloc>(
  38. // create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)),
  39. // child: SearchPageContianer(),
  40. // ),
  41. );
  42. }
  43. }
  44. class SearchPageContianer extends StatefulWidget {
  45. final Map<String, dynamic> data;
  46. const SearchPageContianer({Key key, this.data}) : super(key: key);
  47. @override
  48. _SearchPageContianerState createState() => _SearchPageContianerState();
  49. }
  50. class _SearchPageContianerState extends State<SearchPageContianer> {
  51. /// tab轮播
  52. TabController _tabController;
  53. @override
  54. void initState() {
  55. _tabController = TabController(length: 6, vsync: ScrollableState());
  56. super.initState();
  57. }
  58. @override
  59. void dispose() {
  60. _tabController?.dispose();
  61. super.dispose();
  62. }
  63. @override
  64. Widget build(BuildContext context) {
  65. //设置搜索bar应该选择哪个type的item
  66. Logger.log("数据2: " + json.encode(widget.data));
  67. Logger.log("类型: " + widget.data['default_pvd']);
  68. if(!EmptyUtil.isEmpty(widget.data['default_pvd'])){
  69. Provider.of<SearchTagNotifier>(context, listen: false).setType(widget.data['default_pvd']);
  70. }
  71. return MediaQuery.removePadding(
  72. removeTop: true,
  73. context: context,
  74. child: Container(
  75. width: double.infinity,
  76. child: BlocConsumer<SearchBloc, SearchState>(
  77. listener: (BuildContext context, SearchState state) {
  78. if (state is SearchErrorState) {
  79. print('数据加载出错');
  80. }
  81. },
  82. buildWhen: (previous, current) {
  83. /// 数据加载出错不进行build
  84. if (current is SearchErrorState) {
  85. return false;
  86. }
  87. /// 搜索成功,跳转结果页面
  88. if (current is SearchSubmitSuccessState) {
  89. return false;
  90. }
  91. /// 搜索失败,不进行build
  92. if (current is SearchSubmitErrorState) {
  93. return false;
  94. }
  95. return true;
  96. },
  97. builder: (context, state) {
  98. print('currente state = $state');
  99. if (state is SearchLoadedState) {
  100. return _getMainWidget(state?.model);
  101. }
  102. /// 骨架屏幕
  103. return _getMainWidget(null);
  104. },
  105. ),
  106. ),
  107. );
  108. }
  109. /// 主视图
  110. Widget _getMainWidget(List<Map<String, dynamic>> datas) {
  111. return AnnotatedRegion<SystemUiOverlayStyle>(
  112. value: SystemUiOverlayStyle.dark,
  113. child: Scaffold(
  114. backgroundColor: Colors.white,
  115. body: Listener(
  116. onPointerDown: (down) => RouterUtil.hideKeyboard(context),
  117. child: Column(
  118. children: _createContentWidget(datas),
  119. ),
  120. ),
  121. ),
  122. );
  123. }
  124. List<Widget> _createContentWidget(List<Map<String, dynamic>> datas) {
  125. List<Widget> list = [];
  126. int length = datas?.length ?? 0;
  127. if (length <= 0) {
  128. /// 骨架屏幕?
  129. list.add(Container(
  130. height: 200,
  131. width: double.infinity,
  132. color: Colors.white,
  133. // child: Center(
  134. // child: Text('暂时无数据哦~'),
  135. // ),
  136. ));
  137. } else {
  138. for (int i = 0; i < datas.length; i++) {
  139. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  140. print('item.modName ${item.modName}');
  141. list.addAll(WidgetFactory.create(
  142. item.modName,
  143. isSliver: false,
  144. model: datas[i],
  145. ));
  146. }
  147. }
  148. return list;
  149. }
  150. // List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
  151. // List<Widget> list = List();
  152. //
  153. // int length = datas?.length ?? 0;
  154. //
  155. // if (length <= 0) {
  156. // list.add(SliverToBoxAdapter(
  157. // child: Container(
  158. // height: 200,
  159. // child: Center(
  160. // child: Text('暂时无数据哦~'),
  161. // ),
  162. // ),
  163. // ));
  164. // return list;
  165. // }
  166. //
  167. // for (int i = 0; i < 3; i++) {
  168. // WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  169. // print('item.modName ${item.modName}');
  170. // list.addAll(WidgetFactory.create(
  171. // item.modName,
  172. // isSliver: true,
  173. // model: datas[i],
  174. // ));
  175. // }
  176. //
  177. // return list;
  178. // }
  179. }