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

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