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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 StreamBuilder<List<Map<String, dynamic>>>(
  83. stream: _bloc.outData,
  84. builder: (BuildContext context, AsyncSnapshot snapshot) {
  85. List widgets = _createContent(context, snapshot.data ?? []);
  86. return Scaffold(
  87. backgroundColor: Color(0xfff9f9f9),
  88. floatingActionButton: _floatWidget,
  89. floatingActionButtonLocation:
  90. FloatingActionButtonLocation.centerDocked,
  91. body: MediaQuery.removePadding(
  92. removeTop: true,
  93. context: context,
  94. child: SmartRefresher(
  95. enablePullDown: true,
  96. enablePullUp: false,
  97. header: WaterDropHeader(),
  98. controller: _refreshController,
  99. onLoading: _onLoading,
  100. child: Container(
  101. width: double.infinity,
  102. child: Stack(
  103. fit: StackFit.passthrough,
  104. children: <Widget>[
  105. MineHeaderBgWidget(
  106. controller: _controller,
  107. ),
  108. CustomScrollView(
  109. controller: _controller,
  110. slivers: widgets,
  111. )
  112. ],
  113. ),
  114. ),
  115. ),
  116. ),
  117. );
  118. });
  119. }
  120. List<Widget> _createContent(
  121. BuildContext context, List<Map<String, dynamic>> datas) {
  122. List<Widget> list = List();
  123. for (int i = 0; i < datas.length; i++) {
  124. WidgetModel item =
  125. WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
  126. print('item.modName ${item.modName}');
  127. if (item.modName == 'index_taobao_auth_tip') {
  128. _floatWidget = HomeAuth(datas[i]);
  129. continue;
  130. }
  131. list.addAll(WidgetFactory.create(
  132. item.modName,
  133. isSliver: true,
  134. model: datas[i],
  135. ));
  136. }
  137. if (list.length <= 0) {
  138. list.add(SliverToBoxAdapter(
  139. child: Container(
  140. height: 200,
  141. child: Center(
  142. child: Text('暂时无数据哦~'),
  143. ),
  144. ),
  145. ));
  146. }
  147. return list;
  148. }
  149. }