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

search_page3.dart 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_bloc/flutter_bloc.dart';
  3. import 'bloc/search_bloc.dart';
  4. import 'bloc/search_repository.dart';
  5. import 'package:zhiying_comm/zhiying_comm.dart';
  6. class SearchPage3 extends StatelessWidget {
  7. final Map<String, dynamic> data;
  8. const SearchPage3(this.data);
  9. @override
  10. Widget build(BuildContext context) {
  11. return BlocProvider<SearchBloc>(
  12. create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)),
  13. child: SearchPage3Container(),
  14. );
  15. }
  16. }
  17. class SearchPage3Container extends StatefulWidget {
  18. @override
  19. _SearchPage3ContainerState createState() => _SearchPage3ContainerState();
  20. }
  21. class _SearchPage3ContainerState extends State<SearchPage3Container> {
  22. @override
  23. Widget build(BuildContext context) {
  24. return MediaQuery.removePadding(
  25. removeTop: true,
  26. context: context,
  27. child: Container(
  28. width: double.infinity,
  29. child: BlocConsumer<SearchBloc, SearchState>(
  30. listener: (BuildContext context, SearchState state) {
  31. if (state is SearchErrorState) {
  32. print('数据加载出错');
  33. }
  34. },
  35. buildWhen: (previous, current) {
  36. /// 数据加载出错不进行build
  37. if (current is SearchErrorState) {
  38. return false;
  39. }
  40. /// 搜索成功,跳转结果页面
  41. if (current is SearchSubmitSuccessState) {
  42. return false;
  43. }
  44. /// 搜索失败,不进行build
  45. if (current is SearchSubmitErrorState) {
  46. return false;
  47. }
  48. return true;
  49. },
  50. builder: (context, state) {
  51. print('currente state = $state');
  52. if (state is SearchLoadedState) {
  53. return _getMainWidget(state?.model);
  54. }
  55. /// 骨架屏幕
  56. return _getMainWidget(null);
  57. },
  58. ),
  59. ),
  60. );
  61. }
  62. /// 主视图
  63. Widget _getMainWidget(List<Map<String, dynamic>> datas){
  64. return Scaffold(
  65. backgroundColor: Colors.white,
  66. body: NestedScrollView(
  67. headerSliverBuilder: (context, bool){
  68. return _createHeadWidget(context, datas);
  69. },
  70. body: Container(
  71. child: Text('testssfee'),
  72. ),
  73. ),
  74. );
  75. }
  76. /// 头部视图
  77. List<Widget> _createHeadWidget(BuildContext context, List<Map<String, dynamic>> datas){
  78. List<Widget> list = [];
  79. int length = datas?.length?? 0;
  80. if (length <= 0) {
  81. list.add(SliverToBoxAdapter(
  82. child: Container(
  83. height: 200,
  84. child: Center(
  85. child: Text('暂时无数据哦~'),
  86. ),
  87. ),
  88. ));
  89. return list;
  90. }
  91. for (int i = 0; i < 3; i++) {
  92. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  93. print('item.modName ${item.modName}');
  94. list.addAll(WidgetFactory.create(
  95. item.modName,
  96. isSliver: true,
  97. model: datas[i],
  98. ));
  99. }
  100. return list;
  101. }
  102. /// 子视图
  103. List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
  104. List<Widget> list = List();
  105. /// datas.length - 1 为最后一个在底部
  106. for (int i = 0; i < datas.length; i++) {
  107. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  108. print('item.modName ${item.modName}');
  109. list.addAll(WidgetFactory.create(
  110. item.modName,
  111. isSliver: true,
  112. model: datas[i],
  113. ));
  114. }
  115. if (list.length <= 0) {
  116. list.add(SliverToBoxAdapter(
  117. child: Container(
  118. height: 200,
  119. child: Center(
  120. child: Text('暂时无数据哦~'),
  121. ),
  122. ),
  123. ));
  124. }
  125. return list;
  126. }
  127. }