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

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