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

203 lines
6.3 KiB

  1. import 'dart:ui';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:pull_to_refresh/pull_to_refresh.dart';
  6. import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_create.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/others/mine_header_bg_widget.dart';
  12. import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_header.dart';
  13. import 'package:zhiying_comm/util/base_bloc.dart';
  14. import 'package:zhiying_comm/zhiying_comm.dart';
  15. class MainPage extends StatefulWidget {
  16. final Map<String, dynamic> data;
  17. MainPage(
  18. this.data, {
  19. Key key,
  20. }) : super(key: key);
  21. @override
  22. _MainPageState createState() => _MainPageState();
  23. }
  24. class _MainPageState extends State<MainPage> {
  25. @override
  26. Widget build(BuildContext context) {
  27. return MultiProvider(
  28. providers: [
  29. ChangeNotifierProvider.value(value: MainPageNotifier()),
  30. ChangeNotifierProvider.value(value: MainPageBgNotifier()),
  31. ],
  32. child: BlocProvider<MainPageBloc>(
  33. bloc: MainPageBloc(),
  34. child: _MainPageContainer(widget.data),
  35. ),
  36. );
  37. }
  38. }
  39. class _MainPageContainer extends StatefulWidget {
  40. final Map<String, dynamic> data;
  41. _MainPageContainer(this.data, {Key key}) : super(key: key);
  42. @override
  43. _MainPageContainerState createState() => _MainPageContainerState();
  44. }
  45. class _MainPageContainerState extends State<_MainPageContainer>
  46. with WidgetsBindingObserver {
  47. bool _isEnded = false;
  48. ScrollController _controller = ScrollController();
  49. MainPageBloc _bloc;
  50. RefreshController _refreshController =
  51. 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 didChangeAppLifecycleState(AppLifecycleState state) async {
  67. ///智能粘贴板
  68. String indentifier = widget.data['skip_identifier'];
  69. if (indentifier == 'pub.flutter.index') {
  70. // 只在首页弹窗
  71. IntellectCreate.checkAndCreate(state, context);
  72. }
  73. super.didChangeAppLifecycleState(state);
  74. }
  75. @override
  76. void dispose() {
  77. _controller.dispose();
  78. _refreshController.dispose();
  79. WidgetsBinding.instance.removeObserver(this);
  80. super.dispose();
  81. }
  82. @override
  83. void initState() {
  84. _bloc = BlocProvider.of<MainPageBloc>(context);
  85. if (widget.data.containsKey('skip_identifier')) {
  86. _bloc.loadData(widget.data['skip_identifier']);
  87. }
  88. _controller.addListener(() {
  89. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  90. if (_controller.offset >= _controller.position.maxScrollExtent &&
  91. !_isEnded) {
  92. // 滑动到底部
  93. _isEnded = true;
  94. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  95. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  96. _isEnded) {
  97. _isEnded = false;
  98. Provider.of<MainPageNotifier>(context, listen: false).reset();
  99. }
  100. });
  101. ///监听app生命周期变化
  102. WidgetsBinding.instance.addObserver(this);
  103. IntellectCreate.checkAndCreateFirst(context);
  104. super.initState();
  105. }
  106. @override
  107. Widget build(BuildContext context) {
  108. double top = MediaQueryData.fromWindow(window).padding.top;
  109. return StreamBuilder<List<Map<String, dynamic>>>(
  110. stream: _bloc.outData,
  111. builder: (BuildContext context, AsyncSnapshot snapshot) {
  112. List widgets = _createContent(context, snapshot.data ?? []);
  113. _refreshController.refreshCompleted();
  114. return Scaffold(
  115. backgroundColor: Color(0xfff9f9f9),
  116. floatingActionButton: _floatWidget,
  117. floatingActionButtonLocation:
  118. FloatingActionButtonLocation.centerDocked,
  119. body: MediaQuery.removePadding(
  120. removeTop: true,
  121. context: context,
  122. child: Container(
  123. width: double.infinity,
  124. child: Stack(
  125. fit: StackFit.passthrough,
  126. children: <Widget>[
  127. MineHeaderBgWidget(
  128. controller: _controller,
  129. ),
  130. SmartRefresher(
  131. enablePullDown: true,
  132. enablePullUp: false,
  133. header: RefreshHeader(
  134. offsetY: top,
  135. ),
  136. controller: _refreshController,
  137. onLoading: _onLoading,
  138. onRefresh: _onRefresh,
  139. child: CustomScrollView(
  140. controller: _controller,
  141. slivers: widgets,
  142. ),
  143. )
  144. ],
  145. ),
  146. ),
  147. ),
  148. );
  149. });
  150. }
  151. List<Widget> _createContent(
  152. BuildContext context, List<Map<String, dynamic>> datas) {
  153. List<Widget> list = List();
  154. for (int i = 0; i < datas.length; i++) {
  155. WidgetModel item =
  156. WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  157. print('item.modName ${item.modName}');
  158. if (item.modName == 'index_taobao_auth_tip') {
  159. _floatWidget = HomeAuth(datas[i]);
  160. continue;
  161. }
  162. list.addAll(WidgetFactory.create(
  163. item.modName,
  164. isSliver: true,
  165. model: datas[i],
  166. ));
  167. }
  168. if (list.length <= 0) {
  169. list.add(SliverToBoxAdapter(
  170. child: Container(
  171. height: 200,
  172. child: Center(
  173. child: Text('暂时无数据哦~'),
  174. ),
  175. ),
  176. ));
  177. }
  178. return list;
  179. }
  180. }