基础组件库
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.4 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/dialog/global_dialog/intellect_search_goods_dialog/intellect_search_goods_dialog.dart';
  8. import 'package:zhiying_base_widget/dialog/global_dialog/intellect_search_goods_dialog/intellect_search_no_goods_dialog.dart';
  9. import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
  10. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
  11. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  12. import 'package:zhiying_base_widget/utils/contants.dart';
  13. import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
  14. import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.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(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. with WidgetsBindingObserver {
  49. bool _isEnded = false;
  50. ScrollController _controller = ScrollController();
  51. MainPageBloc _bloc;
  52. RefreshController _refreshController =
  53. RefreshController(initialRefresh: false);
  54. Widget _floatWidget;
  55. void _onLoading() async {
  56. // await Future.delayed(Duration(milliseconds: 1000));
  57. // if (mounted) setState(() {});
  58. // _refreshController.loadComplete();
  59. }
  60. void _onRefresh() async {
  61. // await Future.delayed(Duration(milliseconds: 3000));
  62. // _refreshController.refreshCompleted();
  63. if (widget.data.containsKey('skip_identifier')) {
  64. _bloc.loadData(widget.data['skip_identifier']);
  65. }
  66. }
  67. @override
  68. void didChangeAppLifecycleState(AppLifecycleState state) async {
  69. ///智能粘贴板
  70. IntellectCreate.checkAndCreate(state, context);
  71. super.didChangeAppLifecycleState(state);
  72. }
  73. @override
  74. void dispose() {
  75. _controller.dispose();
  76. _refreshController.dispose();
  77. WidgetsBinding.instance.removeObserver(this);
  78. super.dispose();
  79. }
  80. @override
  81. void initState() {
  82. _bloc = BlocProvider.of<MainPageBloc>(context);
  83. if (widget.data.containsKey('skip_identifier')) {
  84. _bloc.loadData(widget.data['skip_identifier']);
  85. }
  86. _controller.addListener(() {
  87. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  88. if (_controller.offset >= _controller.position.maxScrollExtent &&
  89. !_isEnded) {
  90. // 滑动到底部
  91. _isEnded = true;
  92. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  93. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  94. _isEnded) {
  95. _isEnded = false;
  96. Provider.of<MainPageNotifier>(context, listen: false).reset();
  97. }
  98. });
  99. ///监听app生命周期变化
  100. WidgetsBinding.instance.addObserver(this);
  101. IntellectCreate.checkAndCreateFirst(context);
  102. super.initState();
  103. }
  104. @override
  105. Widget build(BuildContext context) {
  106. double top = MediaQueryData
  107. .fromWindow(window)
  108. .padding
  109. .top;
  110. return StreamBuilder<List<Map<String, dynamic>>>(
  111. stream: _bloc.outData,
  112. builder: (BuildContext context, AsyncSnapshot snapshot) {
  113. List widgets = _createContent(context, snapshot.data ?? []);
  114. _refreshController.refreshCompleted();
  115. return Scaffold(
  116. backgroundColor: Color(0xfff9f9f9),
  117. floatingActionButton: _floatWidget,
  118. floatingActionButtonLocation:
  119. FloatingActionButtonLocation.centerDocked,
  120. body: MediaQuery.removePadding(
  121. removeTop: true,
  122. context: context,
  123. child: Container(
  124. width: double.infinity,
  125. child: Stack(
  126. fit: StackFit.passthrough,
  127. children: <Widget>[
  128. MineHeaderBgWidget(
  129. controller: _controller,
  130. ),
  131. SmartRefresher(
  132. enablePullDown: true,
  133. enablePullUp: false,
  134. header: RefreshHeader(
  135. offsetY: top,
  136. ),
  137. controller: _refreshController,
  138. onLoading: _onLoading,
  139. onRefresh: _onRefresh,
  140. child: CustomScrollView(
  141. controller: _controller,
  142. slivers: widgets,
  143. ),
  144. )
  145. ],
  146. ),
  147. ),
  148. ),
  149. );
  150. });
  151. }
  152. List<Widget> _createContent(BuildContext context,
  153. List<Map<String, dynamic>> datas) {
  154. List<Widget> list = List();
  155. for (int i = 0; i < datas.length; i++) {
  156. WidgetModel item =
  157. WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  158. print('item.modName ${item.modName}');
  159. if (item.modName == 'index_taobao_auth_tip') {
  160. _floatWidget = HomeAuth(datas[i]);
  161. continue;
  162. }
  163. list.addAll(WidgetFactory.create(
  164. item.modName,
  165. isSliver: true,
  166. model: datas[i],
  167. ));
  168. }
  169. if (list.length <= 0) {
  170. list.add(SliverToBoxAdapter(
  171. child: Container(
  172. height: 200,
  173. child: Center(
  174. child: Text('暂时无数据哦~'),
  175. ),
  176. ),
  177. ));
  178. }
  179. return list;
  180. }
  181. }