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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. Logger.log('custom item page current state = ' + state?.toString());
  125. if (EmptyUtil.isEmpty(state.model))
  126. return _buildEmptyWidget();
  127. else
  128. return _buildMainWidget(state.model);
  129. }
  130. if (state is CustomItemPageInitErrorState) {
  131. return _buildEmptyWidget();
  132. }
  133. return _buildSkeletonWidget();
  134. },
  135. );
  136. }
  137. /// 有数据
  138. Widget _buildMainWidget(final List<Map<String, dynamic>> model) {
  139. return MediaQuery.removePadding(
  140. context: context,
  141. removeTop: false,
  142. child: Stack(
  143. children: <Widget>[
  144. SmartRefresher(
  145. controller: _refreshController,
  146. enablePullDown: true,
  147. enablePullUp: true,
  148. onRefresh: _refreshEvent,
  149. onLoading: _loadEvent,
  150. header: RefreshGifHeader(),
  151. // footer: RefreshFooter(),
  152. child: CustomScrollView(
  153. controller: _controller,
  154. slivers: _buildContentWidgets(model),
  155. ),
  156. ),
  157. Align(
  158. alignment: Alignment.bottomCenter,
  159. child: Container(
  160. margin: const EdgeInsets.only(bottom: 8),
  161. child: _buildAuthWidget(model),
  162. ), //_buildAuthWidget(model),
  163. )
  164. ],
  165. ),
  166. );
  167. }
  168. /// 根据widget的modName生成视图
  169. List<Widget> _buildContentWidgets(final List<Map<String, dynamic>> datas) {
  170. List<Widget> result = [];
  171. for (int i = 0; i < datas.length; i++) {
  172. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  173. if (item.modName == 'audit_tip') {
  174. Logger.debug('授权组件,跳过');
  175. continue;
  176. }
  177. if (item.modName == 'product') {
  178. datas[i]['eventBus'] = _eventBus;
  179. }
  180. result.addAll(WidgetFactory.create(
  181. item.modName,
  182. isSliver: true,
  183. model: datas[i],
  184. ));
  185. }
  186. // if (widget.needBuildStatus) {
  187. // double top = MediaQueryData.fromWindow(window).padding.top;
  188. // result.insert(
  189. // 0,
  190. // SliverPersistentHeader(
  191. // pinned: false,
  192. // floating: false,
  193. // delegate: CustomSliverPersistentHeaderDelegate(max: top, min: top, child: Container(
  194. // // color: Colors.yellow,
  195. // color: HexColor.fromHex('#FFFF4242'),
  196. // )),
  197. // ));
  198. //
  199. // }
  200. return result;
  201. }
  202. /// 空数据
  203. Widget _buildEmptyWidget() {
  204. return SmartRefresher(
  205. controller: _refreshController,
  206. onRefresh: _refreshEvent,
  207. enablePullDown: true,
  208. child: Container(
  209. child: EmptyWidget(),
  210. ),
  211. );
  212. }
  213. /// 骨架图
  214. Widget _buildSkeletonWidget() {
  215. return Container();
  216. }
  217. /// 特殊的授权组件
  218. Widget _buildAuthWidget(final List<Map<String, dynamic>> datas) {
  219. int length = datas?.length ?? 0;
  220. if (length == 0) return Container();
  221. Widget rlt;
  222. for (int i = 0; i < datas.length; i++) {
  223. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  224. if (item.modName == 'audit_tip') {
  225. rlt = HomeAuth(datas[i]);
  226. break;
  227. }
  228. }
  229. return rlt ?? Container();
  230. }
  231. }