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

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