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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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/pages/main_page/main_page_bloc.dart';
  6. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
  7. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  8. import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
  9. import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
  10. import 'package:zhiying_comm/util/base_bloc.dart';
  11. import 'package:zhiying_comm/zhiying_comm.dart';
  12. class MainPage extends StatefulWidget {
  13. final Map<String, dynamic> data;
  14. MainPage(
  15. this.data, {
  16. Key key,
  17. }) : super(key: key);
  18. @override
  19. _MainPageState createState() => _MainPageState();
  20. }
  21. class _MainPageState extends State<MainPage> {
  22. @override
  23. Widget build(BuildContext context) {
  24. return MultiProvider(
  25. providers: [
  26. ChangeNotifierProvider.value(value: MainPageNotifier()),
  27. ChangeNotifierProvider.value(value: MainPageBgNotifier()),
  28. ],
  29. child: BlocProvider<MainPageBloc>(
  30. bloc: MainPageBloc(),
  31. child: _MainPageContainer(widget.data),
  32. ),
  33. );
  34. }
  35. }
  36. class _MainPageContainer extends StatefulWidget {
  37. final Map<String, dynamic> data;
  38. _MainPageContainer(this.data, {Key key}) : super(key: key);
  39. @override
  40. _MainPageContainerState createState() => _MainPageContainerState();
  41. }
  42. class _MainPageContainerState extends State<_MainPageContainer> {
  43. bool _isEnded = false;
  44. ScrollController _controller = ScrollController();
  45. MainPageBloc _bloc;
  46. RefreshController _refreshController =
  47. RefreshController(initialRefresh: false);
  48. Widget _floatWidget;
  49. void _onLoading() async {
  50. // await Future.delayed(Duration(milliseconds: 1000));
  51. // if (mounted) setState(() {});
  52. // _refreshController.loadComplete();
  53. }
  54. @override
  55. void dispose() {
  56. _controller.dispose();
  57. super.dispose();
  58. }
  59. @override
  60. void initState() {
  61. _bloc = BlocProvider.of<MainPageBloc>(context);
  62. if (widget.data.containsKey('skip_identifier')) {
  63. _bloc.loadData(widget.data['skip_identifier']);
  64. }
  65. _controller.addListener(() {
  66. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  67. if (_controller.offset >= _controller.position.maxScrollExtent &&
  68. !_isEnded) {
  69. // 滑动到底部
  70. _isEnded = true;
  71. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  72. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  73. _isEnded) {
  74. _isEnded = false;
  75. Provider.of<MainPageNotifier>(context, listen: false).reset();
  76. }
  77. });
  78. super.initState();
  79. }
  80. @override
  81. Widget build(BuildContext context) {
  82. return Scaffold(
  83. backgroundColor: Color(0xfff9f9f9),
  84. floatingActionButton: _floatWidget,
  85. floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
  86. body: MediaQuery.removePadding(
  87. removeTop: true,
  88. context: context,
  89. child: SmartRefresher(
  90. enablePullDown: true,
  91. enablePullUp: false,
  92. header: WaterDropHeader(),
  93. controller: _refreshController,
  94. onLoading: _onLoading,
  95. child: Container(
  96. width: double.infinity,
  97. child: StreamBuilder<List<Map<String, dynamic>>>(
  98. stream: _bloc.outData,
  99. builder: (BuildContext context, AsyncSnapshot snapshot) {
  100. return Stack(
  101. fit: StackFit.passthrough,
  102. children: <Widget>[
  103. MineHeaderBgWidget(
  104. controller: _controller,
  105. ),
  106. CustomScrollView(
  107. controller: _controller,
  108. slivers: _createContent(context, snapshot.data ?? []),
  109. )
  110. ],
  111. );
  112. },
  113. ),
  114. )),
  115. ),
  116. );
  117. }
  118. List<Widget> _createContent(
  119. BuildContext context, List<Map<String, dynamic>> datas) {
  120. List<Widget> list = List();
  121. for (int i = 0; i < datas.length; i++) {
  122. WidgetModel item =
  123. WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  124. print('item.modName ${item.modName}');
  125. if (item.modName == 'index_taobao_auth_tip') {
  126. _floatWidget = HomeAuth(datas[i]);
  127. continue;
  128. }
  129. list.addAll(WidgetFactory.create(
  130. item.modName,
  131. isSliver: true,
  132. model: datas[i],
  133. ));
  134. }
  135. if (list.length <= 0) {
  136. list.add(SliverToBoxAdapter(
  137. child: Container(
  138. height: 200,
  139. child: Center(
  140. child: Text('暂时无数据哦~'),
  141. ),
  142. ),
  143. ));
  144. }
  145. return list;
  146. }
  147. }