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

main_page.dart 5.8 KiB

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