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

custom_item_page.dart 7.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import 'package:event_bus/event_bus.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:pull_to_refresh/pull_to_refresh.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:flutter_bloc/flutter_bloc.dart';
  6. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  7. import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart';
  8. import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
  9. import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_gif_header.dart';
  10. import 'package:zhiying_comm/zhiying_comm.dart';
  11. import 'bloc/custom_item_page_bloc.dart';
  12. import 'bloc/custom_item_page_state.dart';
  13. import 'bloc/custom_item_page_event.dart';
  14. import 'bloc/custom_item_page_repository.dart';
  15. ///
  16. /// 通用模块的分类导航下的子模块
  17. ///
  18. ///
  19. class CustomItemPage extends StatelessWidget {
  20. final Map<String, dynamic> data;
  21. final int tabIndex;
  22. final String modPid;
  23. final String modId;
  24. final bool needBuildStatus;
  25. Function(double) scroller;
  26. CustomItemPage(this.data, this.tabIndex, this.modId, this.modPid, this.needBuildStatus, {this.scroller});
  27. @override
  28. Widget build(BuildContext context) {
  29. return MultiProvider(
  30. providers: [
  31. ChangeNotifierProvider.value(value: MainPageNotifier()),
  32. ],
  33. child: BlocProvider<CustomItemPageBloc>(
  34. create: (_) => CustomItemPageBloc(CustomItemPageRepository(this.data, this.tabIndex, this.modId, this.modPid)),
  35. child: _CustomItemPageContainer(this.data, this.tabIndex, this.modId, this.modPid, this.needBuildStatus,scroller: this.scroller,),
  36. ),
  37. );
  38. }
  39. }
  40. class _CustomItemPageContainer extends StatefulWidget {
  41. final Map<String, dynamic> data;
  42. final int tabIndex;
  43. final String modPid;
  44. final String modId;
  45. final bool needBuildStatus;
  46. final Function(double) scroller;
  47. const _CustomItemPageContainer(this.data, this.tabIndex, this.modId, this.modPid, this.needBuildStatus, {this.scroller, Key key}) : super(key: key);
  48. @override
  49. __CustomItemPageContainerState createState() => __CustomItemPageContainerState();
  50. }
  51. class __CustomItemPageContainerState extends State<_CustomItemPageContainer> with AutomaticKeepAliveClientMixin {
  52. @override
  53. bool get wantKeepAlive => true;
  54. ScrollController _controller;
  55. RefreshController _refreshController;
  56. EventBus _eventBus;
  57. /// 回到顶点
  58. void _scrollTop() {
  59. // _controller.jumpTo(0);
  60. _controller.animateTo(0, duration: Duration(milliseconds: 200), curve: Curves.linear);
  61. }
  62. /// 初始化
  63. void _initEvent() {
  64. BlocProvider.of<CustomItemPageBloc>(context).add(CustomItemPageInitEvent());
  65. }
  66. /// 刷新
  67. void _refreshEvent() {
  68. BlocProvider.of<CustomItemPageBloc>(context).add(CustomItemPageRefreshEvent());
  69. }
  70. /// 下拉更多
  71. void _loadEvent() {
  72. // BlocProvider.of<CustomItemPageBloc>(context).add(CustomItemPageLoadEvent());
  73. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  74. Future.delayed(Duration(milliseconds: 500), () => _refreshController?.loadComplete());
  75. }
  76. @override
  77. void initState() {
  78. _controller = ScrollController();
  79. _eventBus = EventBus();
  80. _refreshController = RefreshController(initialRefresh: false);
  81. _initEvent();_controller.addListener(() {
  82. print(_controller.offset.toString());
  83. if(widget.scroller!=null){
  84. widget?.scroller(_controller.offset);
  85. }
  86. });
  87. super.initState();
  88. }
  89. @override
  90. void dispose() {
  91. _controller?.dispose();
  92. _refreshController?.dispose();
  93. _eventBus?.destroy();
  94. super.dispose();
  95. }
  96. @override
  97. Widget build(BuildContext context) {
  98. return BlocConsumer<CustomItemPageBloc, CustomItemPageState>(
  99. listener: (context, state) {},
  100. buildWhen: (prev, current) {
  101. if (current is CustomItemPageRefreshSuccessState) {
  102. _refreshController?.refreshCompleted(resetFooterState: true);
  103. return false;
  104. }
  105. if (current is CustomItemPageRefreshErrorState) {
  106. _refreshController?.refreshFailed();
  107. return false;
  108. }
  109. if (current is CustomItemPageLoadSuccessState) {
  110. _refreshController?.loadComplete();
  111. return false;
  112. }
  113. if (current is CustomItemPageLoadErrorState) {
  114. _refreshController?.loadNoData();
  115. return false;
  116. }
  117. if (current is CustomItemPageErrorState) {
  118. return false;
  119. }
  120. return true;
  121. },
  122. builder: (context, state) {
  123. if (state is CustomItemPageLoadedState) {
  124. if (EmptyUtil.isEmpty(state.model))
  125. return _buildEmptyWidget();
  126. else
  127. return _buildMainWidget(state.model);
  128. }
  129. if (state is CustomItemPageInitErrorState) {
  130. return _buildEmptyWidget();
  131. }
  132. return _buildSkeletonWidget();
  133. },
  134. );
  135. }
  136. /// 有数据
  137. Widget _buildMainWidget(final List<Map<String, dynamic>> model) {
  138. return MediaQuery.removePadding(
  139. context: context,
  140. removeTop: false,
  141. child: Stack(
  142. children: <Widget>[
  143. SmartRefresher(
  144. controller: _refreshController,
  145. enablePullDown: true,
  146. enablePullUp: true,
  147. onRefresh: _refreshEvent,
  148. onLoading: _loadEvent,
  149. header: RefreshGifHeader(),
  150. // footer: RefreshFooter(),
  151. child: CustomScrollView(
  152. controller: _controller,
  153. slivers: _buildContentWidgets(model),
  154. ),
  155. ),
  156. Align(
  157. alignment: Alignment.bottomCenter,
  158. child: Container(
  159. margin: const EdgeInsets.only(bottom: 8),
  160. child: _buildAuthWidget(model),
  161. ), //_buildAuthWidget(model),
  162. )
  163. ],
  164. ),
  165. );
  166. }
  167. /// 根据widget的modName生成视图
  168. List<Widget> _buildContentWidgets(final List<Map<String, dynamic>> datas) {
  169. List<Widget> result = [];
  170. for (int i = 0; i < datas.length; i++) {
  171. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  172. if (item.modName == 'audit_tip') {
  173. Logger.debug('授权组件,跳过');
  174. continue;
  175. }
  176. if (item.modName == 'product') {
  177. datas[i]['eventBus'] = _eventBus;
  178. }
  179. result.addAll(WidgetFactory.create(
  180. item.modName,
  181. isSliver: true,
  182. model: datas[i],
  183. ));
  184. }
  185. // if (widget.needBuildStatus) {
  186. // double top = MediaQueryData.fromWindow(window).padding.top;
  187. // result.insert(
  188. // 0,
  189. // SliverPersistentHeader(
  190. // pinned: false,
  191. // floating: false,
  192. // delegate: CustomSliverPersistentHeaderDelegate(max: top, min: top, child: Container(
  193. // // color: Colors.yellow,
  194. // color: HexColor.fromHex('#FFFF4242'),
  195. // )),
  196. // ));
  197. //
  198. // }
  199. return result;
  200. }
  201. /// 空数据
  202. Widget _buildEmptyWidget() {
  203. return SmartRefresher(
  204. controller: _refreshController,
  205. onRefresh: _refreshEvent,
  206. enablePullDown: true,
  207. child: Container(
  208. child: EmptyWidget(),
  209. ),
  210. );
  211. }
  212. /// 骨架图
  213. Widget _buildSkeletonWidget() {
  214. return Container();
  215. }
  216. /// 特殊的授权组件
  217. Widget _buildAuthWidget(final List<Map<String, dynamic>> datas) {
  218. int length = datas?.length ?? 0;
  219. if (length == 0) return Container();
  220. Widget rlt;
  221. for (int i = 0; i < datas.length; i++) {
  222. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  223. if (item.modName == 'audit_tip') {
  224. rlt = HomeAuth(datas[i]);
  225. break;
  226. }
  227. }
  228. return rlt ?? Container();
  229. }
  230. }