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

302 lines
9.1 KiB

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