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

183 lines
5.5 KiB

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
  4. import 'package:zhiying_base_widget/pages/main_page/main_page_notifier.dart';
  5. import 'package:zhiying_base_widget/pages/main_page/main_page_sk.dart';
  6. import 'package:zhiying_comm/zhiying_comm.dart';
  7. import 'package:provider/provider.dart';
  8. import 'package:zhiying_comm/util/base_bloc.dart';
  9. import 'package:pull_to_refresh/pull_to_refresh.dart';
  10. class MainPage extends StatefulWidget {
  11. final Map<String, dynamic> data;
  12. MainPage(
  13. this.data, {
  14. Key key,
  15. }) : super(key: key);
  16. @override
  17. _MainPageState createState() => _MainPageState();
  18. }
  19. class _MainPageState extends State<MainPage> {
  20. @override
  21. Widget build(BuildContext context) {
  22. return Scaffold(
  23. backgroundColor: Color(0xfff9f9f9),
  24. body: ChangeNotifierProvider(
  25. create: (context) => MainPageNotifier(),
  26. child: BlocProvider<MainPageBloc>(
  27. bloc: MainPageBloc(),
  28. child: _MainPageContainer(widget.data),
  29. ),
  30. ),
  31. );
  32. }
  33. }
  34. class _MainPageContainer extends StatefulWidget {
  35. final Map<String, dynamic> data;
  36. _MainPageContainer(this.data, {Key key}) : super(key: key);
  37. @override
  38. _MainPageContainerState createState() => _MainPageContainerState();
  39. }
  40. class _MainPageContainerState extends State<_MainPageContainer> {
  41. WidgetType _type = WidgetType.normal;
  42. bool _isEnded = false;
  43. ScrollController _controller = ScrollController();
  44. MainPageBloc _bloc;
  45. List<String> items = ["1", "2", "3", "4", "5", "6", "7", "8"];
  46. RefreshController _refreshController =
  47. RefreshController(initialRefresh: false);
  48. void _onRefresh() async {
  49. // monitor network fetch
  50. await Future.delayed(Duration(milliseconds: 1000));
  51. // if failed,use refreshFailed()
  52. _refreshController.refreshCompleted();
  53. }
  54. void _onLoading() async {
  55. // monitor network fetch
  56. await Future.delayed(Duration(milliseconds: 1000));
  57. // if failed,use loadFailed(),if no data return,use LoadNodata()
  58. items.add((items.length + 1).toString());
  59. if (mounted) setState(() {});
  60. _refreshController.loadComplete();
  61. }
  62. @override
  63. void dispose() {
  64. _controller.dispose();
  65. super.dispose();
  66. }
  67. @override
  68. void initState() {
  69. _bloc = BlocProvider.of<MainPageBloc>(context);
  70. _reload();
  71. _controller.addListener(() {
  72. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  73. if (_controller.offset >= _controller.position.maxScrollExtent &&
  74. !_isEnded) {
  75. // 滑动到底部
  76. _isEnded = true;
  77. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  78. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  79. _isEnded) {
  80. _isEnded = false;
  81. Provider.of<MainPageNotifier>(context, listen: false).reset();
  82. }
  83. });
  84. super.initState();
  85. }
  86. void _reload() {
  87. setState(() {
  88. _type = WidgetType.skeleton;
  89. });
  90. WidgetModel model = WidgetModel.fromJson(widget.data);
  91. if ((model?.components?.length ?? 0) > 0) {
  92. _bloc.loadData(model.components.map((item) => item.modId).toList());
  93. }
  94. // Future.delayed(Duration(seconds: 3), () {
  95. // setState(() {
  96. // _type = WidgetType.normal;
  97. // });
  98. // });
  99. }
  100. List<Widget> _createContent(BuildContext context) {
  101. List<Widget> list = List();
  102. list.add(SliverToBoxAdapter(
  103. child: RaisedButton(
  104. onPressed: () {
  105. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  106. _reload();
  107. },
  108. child: Text('重置'),
  109. ),
  110. ));
  111. list.addAll(WidgetFactory.create('index_carousel',
  112. type: _type,
  113. isSliver: true,
  114. model: Map<String, dynamic>.from({'aaaaa': 'bbbb'})));
  115. list.addAll(WidgetFactory.create('home_quick_entry', type: _type, isSliver: true));
  116. list.addAll(WidgetFactory.create('index_recommend_list',
  117. type: _type, isSliver: true));
  118. return list;
  119. }
  120. @override
  121. Widget build(BuildContext context) {
  122. return StreamBuilder<List<List<Map<String, dynamic>>>>(
  123. stream: _bloc.outData,
  124. builder: (BuildContext context, AsyncSnapshot snapshot) {
  125. if (snapshot.data == null) {
  126. return MainPageSkeleton();
  127. }
  128. _type = WidgetType.normal;
  129. return SmartRefresher(
  130. enablePullDown: true,
  131. enablePullUp: true,
  132. header: WaterDropHeader(),
  133. footer: CustomFooter(
  134. builder: (BuildContext context, LoadStatus mode) {
  135. Widget body;
  136. if (mode == LoadStatus.idle) {
  137. body = Text("上拉加载");
  138. } else if (mode == LoadStatus.loading) {
  139. body = CupertinoActivityIndicator();
  140. } else if (mode == LoadStatus.failed) {
  141. body = Text("加载失败!点击重试!");
  142. } else if (mode == LoadStatus.canLoading) {
  143. body = Text("松手,加载更多!");
  144. } else {
  145. body = Text("没有更多数据了!");
  146. }
  147. return Container(
  148. height: 55.0,
  149. child: Center(child: body),
  150. );
  151. },
  152. ),
  153. controller: _refreshController,
  154. onRefresh: _onRefresh,
  155. onLoading: _onLoading,
  156. child: CustomScrollView(
  157. controller: _controller,
  158. slivers: _createContent(context),
  159. ),
  160. );
  161. },
  162. );
  163. }
  164. }