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

138 lines
3.4 KiB

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