基础组件库
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

186 linhas
5.0 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. width: double.infinity,
  119. color: Colors.white,
  120. // child: Center(
  121. // child: Text('暂时无数据哦~'),
  122. // ),
  123. ));
  124. } else {
  125. for (int i = 0; i < datas.length; i++) {
  126. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  127. print('item.modName ${item.modName}');
  128. list.addAll(WidgetFactory.create(
  129. item.modName,
  130. isSliver: false,
  131. model: datas[i],
  132. ));
  133. }
  134. }
  135. return list;
  136. }
  137. // List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
  138. // List<Widget> list = List();
  139. //
  140. // int length = datas?.length ?? 0;
  141. //
  142. // if (length <= 0) {
  143. // list.add(SliverToBoxAdapter(
  144. // child: Container(
  145. // height: 200,
  146. // child: Center(
  147. // child: Text('暂时无数据哦~'),
  148. // ),
  149. // ),
  150. // ));
  151. // return list;
  152. // }
  153. //
  154. // for (int i = 0; i < 3; i++) {
  155. // WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  156. // print('item.modName ${item.modName}');
  157. // list.addAll(WidgetFactory.create(
  158. // item.modName,
  159. // isSliver: true,
  160. // model: datas[i],
  161. // ));
  162. // }
  163. //
  164. // return list;
  165. // }
  166. }