基础组件库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

181 wiersze
4.8 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: Column(
  104. children: _createContentWidget(datas),
  105. ),
  106. );
  107. }
  108. List<Widget> _createContentWidget(List<Map<String, dynamic>> datas) {
  109. List<Widget> list = [];
  110. int length = datas?.length ?? 0;
  111. if (length <= 0) {
  112. /// 骨架屏幕?
  113. list.add(Container(
  114. height: 200,
  115. child: Center(
  116. child: Text('暂时无数据哦~'),
  117. ),
  118. ));
  119. } else {
  120. for (int i = 0; i < datas.length; i++) {
  121. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  122. print('item.modName ${item.modName}');
  123. list.addAll(WidgetFactory.create(
  124. item.modName,
  125. isSliver: false,
  126. model: datas[i],
  127. ));
  128. }
  129. }
  130. return list;
  131. }
  132. // List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
  133. // List<Widget> list = List();
  134. //
  135. // int length = datas?.length ?? 0;
  136. //
  137. // if (length <= 0) {
  138. // list.add(SliverToBoxAdapter(
  139. // child: Container(
  140. // height: 200,
  141. // child: Center(
  142. // child: Text('暂时无数据哦~'),
  143. // ),
  144. // ),
  145. // ));
  146. // return list;
  147. // }
  148. //
  149. // for (int i = 0; i < 3; i++) {
  150. // WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  151. // print('item.modName ${item.modName}');
  152. // list.addAll(WidgetFactory.create(
  153. // item.modName,
  154. // isSliver: true,
  155. // model: datas[i],
  156. // ));
  157. // }
  158. //
  159. // return list;
  160. // }
  161. }