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

184 lines
4.4 KiB

  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 SearchPage2 extends StatelessWidget {
  7. final Map<String, dynamic> data;
  8. const SearchPage2(this.data);
  9. @override
  10. Widget build(BuildContext context) {
  11. return BlocProvider<SearchBloc>(
  12. create: (_) => SearchBloc(repository: SearchRepository())..add(SearchInitEvent(model: data)),
  13. child: SearchPage2Container(),
  14. );
  15. }
  16. }
  17. class SearchPage2Container extends StatefulWidget {
  18. @override
  19. _SearchPage2ContainerState createState() => _SearchPage2ContainerState();
  20. }
  21. class _SearchPage2ContainerState extends State<SearchPage2Container> {
  22. TabController _controller;
  23. @override
  24. void initState() {
  25. _controller = TabController(length: tabTitle.length, vsync: ScrollableState());
  26. super.initState();
  27. }
  28. @override
  29. void dispose() {
  30. _controller?.dispose();
  31. super.dispose();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. return MediaQuery.removePadding(
  36. removeTop: true,
  37. context: context,
  38. child: Container(
  39. width: double.infinity,
  40. child: BlocConsumer<SearchBloc, SearchState>(
  41. listener: (BuildContext context, SearchState state) {
  42. if (state is SearchErrorState) {
  43. print('数据加载出错');
  44. }
  45. },
  46. buildWhen: (previous, current) {
  47. /// 数据加载出错不进行build
  48. if (current is SearchErrorState) {
  49. return false;
  50. }
  51. /// 搜索成功,跳转结果页面
  52. if (current is SearchSubmitSuccessState) {
  53. return false;
  54. }
  55. /// 搜索失败,不进行build
  56. if (current is SearchSubmitErrorState) {
  57. return false;
  58. }
  59. return true;
  60. },
  61. builder: (context, state) {
  62. print('currente state = $state');
  63. if (state is SearchLoadedState) {
  64. return _getMainWidget(state?.model);
  65. }
  66. /// 骨架屏幕
  67. return _getMainWidget(null);
  68. },
  69. ),
  70. ),
  71. );
  72. }
  73. /// 主视图
  74. Widget _getMainWidget(List<Map<String, dynamic>> datas){
  75. return Scaffold(
  76. backgroundColor: Colors.white,
  77. body: NestedScrollView(
  78. headerSliverBuilder: (context, bool){
  79. return _createHeadWidget(context, datas);
  80. },
  81. body: ListView(
  82. children: <Widget>[
  83. Text('sss')
  84. ],
  85. ),
  86. ),
  87. );
  88. }
  89. /// 头部视图
  90. List<Widget> _createHeadWidget(BuildContext context, List<Map<String, dynamic>> datas){
  91. List<Widget> list = [];
  92. int length = datas?.length?? 0;
  93. if (length <= 0) {
  94. list.add(SliverToBoxAdapter(
  95. child: Container(
  96. height: 200,
  97. child: Center(
  98. child: Text('暂时无数据哦~'),
  99. ),
  100. ),
  101. ));
  102. return list;
  103. }
  104. for (int i = 0; i < 2; i++) {
  105. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  106. print('item.modName ${item.modName}');
  107. list.addAll(WidgetFactory.create(
  108. item.modName,
  109. isSliver: true,
  110. model: datas[i],
  111. ));
  112. }
  113. list.add(_getTabbar());
  114. return list;
  115. }
  116. var tabTitle = [
  117. '页面1',
  118. '页面2',
  119. '页面3',
  120. ];
  121. Widget _getTabbar(){
  122. /// 悬停TabBar
  123. return SliverPersistentHeader(
  124. delegate: new SliverTabBarDelegate(
  125. TabBar(
  126. controller: _controller,
  127. tabs: tabTitle.map((f) => Tab(text: f)).toList(),
  128. indicatorColor: Colors.red,
  129. unselectedLabelColor: Colors.black,
  130. labelColor: Colors.red,
  131. ),
  132. ),
  133. pinned: true,
  134. );
  135. }
  136. }
  137. class SliverTabBarDelegate extends SliverPersistentHeaderDelegate {
  138. final TabBar widget;
  139. const SliverTabBarDelegate(this.widget)
  140. : assert(widget != null);
  141. @override
  142. Widget build(
  143. BuildContext context, double shrinkOffset, bool overlapsContent) {
  144. return Container( color:Colors.white,child: this.widget);
  145. }
  146. @override
  147. bool shouldRebuild(SliverTabBarDelegate oldDelegate) {
  148. return false;
  149. }
  150. @override
  151. double get maxExtent => widget.preferredSize.height;
  152. @override
  153. double get minExtent => widget.preferredSize.height;
  154. }