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

190 lines
5.2 KiB

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