基础组件库
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 

129 wiersze
3.6 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_comm/zhiying_comm.dart';
  6. import 'package:provider/provider.dart';
  7. import 'package:zhiying_comm/util/base_bloc.dart';
  8. import 'package:pull_to_refresh/pull_to_refresh.dart';
  9. class MainPage extends StatefulWidget {
  10. final Map<String, dynamic> data;
  11. MainPage(
  12. this.data, {
  13. Key key,
  14. }) : super(key: key);
  15. @override
  16. _MainPageState createState() => _MainPageState();
  17. }
  18. class _MainPageState extends State<MainPage> {
  19. @override
  20. Widget build(BuildContext context) {
  21. return Scaffold(
  22. backgroundColor: Color(0xfff9f9f9),
  23. body: SafeArea(
  24. child: ChangeNotifierProvider(
  25. create: (context) => MainPageNotifier(),
  26. child: BlocProvider<MainPageBloc>(
  27. bloc: MainPageBloc(),
  28. child: _MainPageContainer(widget.data),
  29. ),
  30. ),
  31. ),
  32. );
  33. }
  34. }
  35. class _MainPageContainer extends StatefulWidget {
  36. final Map<String, dynamic> data;
  37. _MainPageContainer(this.data, {Key key}) : super(key: key);
  38. @override
  39. _MainPageContainerState createState() => _MainPageContainerState();
  40. }
  41. class _MainPageContainerState extends State<_MainPageContainer> {
  42. bool _isEnded = false;
  43. ScrollController _controller = ScrollController();
  44. MainPageBloc _bloc;
  45. RefreshController _refreshController =
  46. RefreshController(initialRefresh: false);
  47. void _onLoading() async {
  48. // await Future.delayed(Duration(milliseconds: 1000));
  49. // if (mounted) setState(() {});
  50. // _refreshController.loadComplete();
  51. }
  52. @override
  53. void dispose() {
  54. _controller.dispose();
  55. super.dispose();
  56. }
  57. @override
  58. void initState() {
  59. _bloc = BlocProvider.of<MainPageBloc>(context);
  60. _controller.addListener(() {
  61. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  62. if (_controller.offset >= _controller.position.maxScrollExtent &&
  63. !_isEnded) {
  64. // 滑动到底部
  65. _isEnded = true;
  66. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  67. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  68. _isEnded) {
  69. _isEnded = false;
  70. Provider.of<MainPageNotifier>(context, listen: false).reset();
  71. }
  72. });
  73. super.initState();
  74. }
  75. @override
  76. Widget build(BuildContext context) {
  77. return StreamBuilder<Map<String, dynamic>>(
  78. stream: _bloc.outData,
  79. builder: (BuildContext context, AsyncSnapshot snapshot) {
  80. return SmartRefresher(
  81. enablePullDown: true,
  82. enablePullUp: false,
  83. header: WaterDropHeader(),
  84. controller: _refreshController,
  85. onLoading: _onLoading,
  86. child: CustomScrollView(
  87. controller: _controller,
  88. slivers: _createContent(context),
  89. ),
  90. );
  91. },
  92. );
  93. }
  94. List<Widget> _createContent(BuildContext context) {
  95. WidgetModel model = WidgetModel.fromJson(widget.data);
  96. List<Widget> list = List();
  97. for (int i = 0; i < model.components.length; i++) {
  98. WidgetModel item = model.components[i];
  99. Map<String, dynamic> data = null;
  100. if (widget.data.containsKey('components')) {
  101. data = widget.data['components'][i];
  102. }
  103. list.addAll(WidgetFactory.create(
  104. item.modName,
  105. type: WidgetType.skeleton,
  106. isSliver: true,
  107. model: data,
  108. ));
  109. }
  110. model.components.forEach((item) {});
  111. return list;
  112. }
  113. }