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

191 lines
6.1 KiB

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