基础组件库
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

search_page.dart 5.5 KiB

hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. if(!EmptyUtil.isEmpty(widget.data['default_pvd'])){
  68. Provider.of<SearchTagNotifier>(context, listen: false).setType(widget.data['default_pvd']);
  69. }
  70. return MediaQuery.removePadding(
  71. removeTop: true,
  72. context: context,
  73. child: Container(
  74. width: double.infinity,
  75. child: BlocConsumer<SearchBloc, SearchState>(
  76. listener: (BuildContext context, SearchState state) {
  77. if (state is SearchErrorState) {
  78. print('数据加载出错');
  79. }
  80. },
  81. buildWhen: (previous, current) {
  82. /// 数据加载出错不进行build
  83. if (current is SearchErrorState) {
  84. return false;
  85. }
  86. /// 搜索成功,跳转结果页面
  87. if (current is SearchSubmitSuccessState) {
  88. return false;
  89. }
  90. /// 搜索失败,不进行build
  91. if (current is SearchSubmitErrorState) {
  92. return false;
  93. }
  94. return true;
  95. },
  96. builder: (context, state) {
  97. print('currente state = $state');
  98. if (state is SearchLoadedState) {
  99. return _getMainWidget(state?.model);
  100. }
  101. /// 骨架屏幕
  102. return _getMainWidget(null);
  103. },
  104. ),
  105. ),
  106. );
  107. }
  108. /// 主视图
  109. Widget _getMainWidget(List<Map<String, dynamic>> datas) {
  110. return AnnotatedRegion<SystemUiOverlayStyle>(
  111. value: SystemUiOverlayStyle.dark,
  112. child: Scaffold(
  113. backgroundColor: Colors.white,
  114. body: Listener(
  115. onPointerDown: (down) => RouterUtil.hideKeyboard(context),
  116. child: Column(
  117. children: _createContentWidget(datas),
  118. ),
  119. ),
  120. ),
  121. );
  122. }
  123. List<Widget> _createContentWidget(List<Map<String, dynamic>> datas) {
  124. List<Widget> list = [];
  125. int length = datas?.length ?? 0;
  126. if (length <= 0) {
  127. /// 骨架屏幕?
  128. list.add(Container(
  129. height: 200,
  130. width: double.infinity,
  131. color: Colors.white,
  132. // child: Center(
  133. // child: Text('暂时无数据哦~'),
  134. // ),
  135. ));
  136. } else {
  137. for (int i = 0; i < datas.length; i++) {
  138. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  139. print('item.modName ${item.modName}');
  140. list.addAll(WidgetFactory.create(
  141. item.modName,
  142. isSliver: false,
  143. model: datas[i],
  144. ));
  145. }
  146. }
  147. return list;
  148. }
  149. // List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
  150. // List<Widget> list = List();
  151. //
  152. // int length = datas?.length ?? 0;
  153. //
  154. // if (length <= 0) {
  155. // list.add(SliverToBoxAdapter(
  156. // child: Container(
  157. // height: 200,
  158. // child: Center(
  159. // child: Text('暂时无数据哦~'),
  160. // ),
  161. // ),
  162. // ));
  163. // return list;
  164. // }
  165. //
  166. // for (int i = 0; i < 3; i++) {
  167. // WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  168. // print('item.modName ${item.modName}');
  169. // list.addAll(WidgetFactory.create(
  170. // item.modName,
  171. // isSliver: true,
  172. // model: datas[i],
  173. // ));
  174. // }
  175. //
  176. // return list;
  177. // }
  178. }