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

184 lines
4.9 KiB

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