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

185 lines
5.6 KiB

  1. import 'dart:ui';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:provider/provider.dart';
  5. import 'package:pull_to_refresh/pull_to_refresh.dart';
  6. import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
  7. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart';
  8. import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
  9. import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
  10. import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart';
  11. import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_header.dart';
  12. import 'package:zhiying_comm/util/base_bloc.dart';
  13. import 'package:zhiying_comm/zhiying_comm.dart';
  14. class MainPage extends StatefulWidget {
  15. final Map<String, dynamic> data;
  16. MainPage(
  17. this.data, {
  18. Key key,
  19. }) : super(key: key);
  20. @override
  21. _MainPageState createState() => _MainPageState();
  22. }
  23. class _MainPageState extends State<MainPage> {
  24. @override
  25. Widget build(BuildContext context) {
  26. return MultiProvider(
  27. providers: [
  28. ChangeNotifierProvider.value(value: MainPageNotifier()),
  29. ChangeNotifierProvider.value(value: MainPageBgNotifier()),
  30. ],
  31. child: BlocProvider<MainPageBloc>(
  32. bloc: MainPageBloc(),
  33. child: _MainPageContainer(widget.data),
  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. Widget _floatWidget;
  51. void _onLoading() async {
  52. // await Future.delayed(Duration(milliseconds: 1000));
  53. // if (mounted) setState(() {});
  54. // _refreshController.loadComplete();
  55. }
  56. void _onRefresh() async {
  57. // await Future.delayed(Duration(milliseconds: 3000));
  58. // _refreshController.refreshCompleted();
  59. if (widget.data.containsKey('skip_identifier')) {
  60. _bloc.loadData(widget.data['skip_identifier']);
  61. }
  62. }
  63. @override
  64. void dispose() {
  65. _controller.dispose();
  66. _refreshController.dispose();
  67. super.dispose();
  68. }
  69. @override
  70. void initState() {
  71. _bloc = BlocProvider.of<MainPageBloc>(context);
  72. if (widget.data.containsKey('skip_identifier')) {
  73. _bloc.loadData(widget.data['skip_identifier']);
  74. }
  75. _controller.addListener(() {
  76. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  77. if (_controller.offset >= _controller.position.maxScrollExtent &&
  78. !_isEnded) {
  79. // 滑动到底部
  80. _isEnded = true;
  81. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  82. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  83. _isEnded) {
  84. _isEnded = false;
  85. Provider.of<MainPageNotifier>(context, listen: false).reset();
  86. }
  87. });
  88. super.initState();
  89. }
  90. @override
  91. Widget build(BuildContext context) {
  92. double top = MediaQueryData.fromWindow(window).padding.top;
  93. return StreamBuilder<List<Map<String, dynamic>>>(
  94. stream: _bloc.outData,
  95. builder: (BuildContext context, AsyncSnapshot snapshot) {
  96. List widgets = _createContent(context, snapshot.data ?? []);
  97. _refreshController.refreshCompleted();
  98. return Scaffold(
  99. backgroundColor: Color(0xfff9f9f9),
  100. floatingActionButton: _floatWidget,
  101. floatingActionButtonLocation:
  102. FloatingActionButtonLocation.centerDocked,
  103. body: MediaQuery.removePadding(
  104. removeTop: true,
  105. context: context,
  106. child: Container(
  107. width: double.infinity,
  108. child: Stack(
  109. fit: StackFit.passthrough,
  110. children: <Widget>[
  111. MineHeaderBgWidget(
  112. controller: _controller,
  113. ),
  114. SmartRefresher(
  115. enablePullDown: true,
  116. enablePullUp: false,
  117. header: RefreshHeader(
  118. offsetY: top,
  119. ),
  120. controller: _refreshController,
  121. onLoading: _onLoading,
  122. onRefresh: _onRefresh,
  123. child: 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. }