基础组件库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

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