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

235 lines
7.8 KiB

  1. import 'dart:convert';
  2. import 'dart:ui';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:provider/provider.dart';
  6. import 'package:pull_to_refresh/pull_to_refresh.dart';
  7. import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
  8. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
  9. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  10. import 'package:zhiying_base_widget/widgets/base_state/base_state.dart';
  11. import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
  12. import 'package:zhiying_base_widget/widgets/mine/mine_nav/mine_nav_bg.dart';
  13. import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
  14. import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_gif_header.dart';
  15. import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_header.dart';
  16. import 'package:zhiying_comm/util/base_bloc.dart';
  17. import 'package:zhiying_comm/zhiying_comm.dart';
  18. class MainPage extends StatefulWidget {
  19. final Map<String, dynamic> data;
  20. MainPage(
  21. this.data, {
  22. Key key,
  23. }) : super(key: key);
  24. @override
  25. _MainPageState createState() => _MainPageState();
  26. }
  27. class _MainPageState extends State<MainPage> {
  28. @override
  29. Widget build(BuildContext context) {
  30. return MultiProvider(
  31. providers: [
  32. ChangeNotifierProvider.value(value: MainPageNotifier()),
  33. ChangeNotifierProvider.value(value: MainPageBgNotifier()),
  34. ],
  35. child: BlocProvider<MainPageBloc>(
  36. bloc: MainPageBloc(),
  37. child: _MainPageContainer(widget.data),
  38. ),
  39. );
  40. }
  41. }
  42. class _MainPageContainer extends StatefulWidget {
  43. final Map<String, dynamic> data;
  44. _MainPageContainer(this.data, {Key key}) : super(key: key);
  45. @override
  46. _MainPageContainerState createState() => _MainPageContainerState();
  47. }
  48. class _MainPageContainerState extends BasePageState<_MainPageContainer> {
  49. bool _isEnded = false;
  50. ScrollController _controller = ScrollController();
  51. MainPageBloc _bloc;
  52. RefreshController _refreshController = RefreshController(initialRefresh: false);
  53. Widget _floatWidget;
  54. void _onLoading() async {
  55. // await Future.delayed(Duration(milliseconds: 1000));
  56. // if (mounted) setState(() {});
  57. // _refreshController.loadComplete();
  58. }
  59. void _onRefresh() async {
  60. // await Future.delayed(Duration(milliseconds: 3000));
  61. // _refreshController.refreshCompleted();
  62. if (widget.data.containsKey('skip_identifier')) {
  63. _bloc.loadData(widget.data['skip_identifier']);
  64. refreshPage();
  65. }
  66. }
  67. @override
  68. void dispose() {
  69. _controller.dispose();
  70. _refreshController.dispose();
  71. super.dispose();
  72. }
  73. @override
  74. void initState() {
  75. _bloc = BlocProvider.of<MainPageBloc>(context);
  76. if (widget.data.containsKey('skip_identifier')) {
  77. _bloc.loadData(widget.data['skip_identifier']);
  78. }
  79. _controller.addListener(() {
  80. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  81. if (_controller.offset >= _controller.position.maxScrollExtent && !_isEnded) {
  82. // 滑动到底部
  83. _isEnded = true;
  84. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  85. } else if (_controller.offset < _controller.position.maxScrollExtent && _isEnded) {
  86. _isEnded = false;
  87. Provider.of<MainPageNotifier>(context, listen: false).reset();
  88. }
  89. });
  90. super.initState();
  91. }
  92. @override
  93. Widget buildX(BuildContext context) {
  94. double top = MediaQueryData.fromWindow(window).padding.top;
  95. return StreamBuilder<List<Map<String, dynamic>>>(
  96. stream: _bloc.outData,
  97. builder: (BuildContext context, AsyncSnapshot snapshot) {
  98. print("mainPageBuild");
  99. List widgets = _createContent(context, snapshot.data ?? []);
  100. _refreshController.refreshCompleted();
  101. Widget bgWidget = validateBgWidget(context, snapshot.data ?? []);
  102. return Scaffold(
  103. backgroundColor: HexColor.fromHex("#fff9f9f9"),
  104. floatingActionButton: _floatWidget,
  105. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  106. body: MediaQuery.removePadding(
  107. removeTop: true,
  108. context: context,
  109. child: Container(
  110. width: double.infinity,
  111. child: Stack(
  112. fit: StackFit.passthrough,
  113. children: <Widget>[
  114. MineHeaderBgWidget(
  115. bgWidget: bgWidget,
  116. controller: _controller,
  117. ),
  118. SmartRefresher(
  119. enablePullDown: true,
  120. enablePullUp: false,
  121. header: RefreshGifHeader(),
  122. // RefreshHeader(
  123. // offsetY: top,
  124. // ),
  125. controller: _refreshController,
  126. onLoading: _onLoading,
  127. onRefresh: _onRefresh,
  128. child: CustomScrollView(
  129. controller: _controller,
  130. slivers: widgets,
  131. ),
  132. )
  133. ],
  134. ),
  135. ),
  136. ),
  137. );
  138. });
  139. }
  140. List<Widget> _createContent(BuildContext context, List<Map<String, dynamic>> datas) {
  141. List<Widget> list = List();
  142. for (int i = 0; i < datas.length; i++) {
  143. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  144. print('item.modName ${item.modName}');
  145. if (item.modName == 'index_taobao_auth_tip') {
  146. _floatWidget = HomeAuth(datas[i]);
  147. continue;
  148. }
  149. list.addAll(WidgetFactory.create(
  150. item.modName,
  151. isSliver: true,
  152. model: datas[i],
  153. ));
  154. }
  155. if (list.length <= 0) {
  156. list.add(SliverToBoxAdapter(
  157. child: Container(
  158. height: 200,
  159. // child: Center(
  160. // child: Text('暂时无数据哦~'),
  161. // ),
  162. ),
  163. ));
  164. }
  165. return list;
  166. }
  167. ///处理特殊背景图
  168. Widget validateBgWidget(BuildContext context, List<Map<String, dynamic>> datas) {
  169. for (int i = 0; i < datas.length; i++) {
  170. WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  171. if (item.modName == "profile_background") {
  172. final double statusBarHeight = MediaQuery.of(context).padding.top;
  173. String url = json.decode(item.data)['img'];
  174. return Container(
  175. width: double.infinity,
  176. height: statusBarHeight + 250,
  177. child: CachedNetworkImage(
  178. imageUrl: url,
  179. fit: BoxFit.fitHeight,
  180. ),
  181. );
  182. }
  183. // if (item.modName == "member_info") {
  184. // final double statusBarHeight = MediaQuery.of(context).padding.top;
  185. // String url = json.decode(item.data)['bg_image'];
  186. // String bgColor = json.decode(item.data)['bg_color'];
  187. // return Container(
  188. // width: double.infinity,
  189. // height: statusBarHeight + 250,
  190. // color: HexColor.fromHex(bgColor ?? ""),
  191. // child: CachedNetworkImage(
  192. // imageUrl: url ?? "",
  193. // fit: BoxFit.fitHeight,
  194. // ),
  195. // );
  196. // }
  197. }
  198. if (_bloc.backgroundModel != null) {
  199. var headerBg = _bloc.backgroundModel.headerBg;
  200. return Container(
  201. height: double.tryParse(_bloc?.backgroundModel?.headerBg?.height) ?? 0,
  202. decoration: BoxDecoration(
  203. gradient: LinearGradient(
  204. begin: Alignment.topCenter,
  205. end: Alignment.bottomCenter,
  206. colors: [HexColor.fromHex(headerBg?.mainColor ?? ""), HexColor.fromHex(headerBg?.assistColor ?? ""), HexColor.fromHex(headerBg?.minorColor ?? "")])),
  207. );
  208. }
  209. return null;
  210. }
  211. }