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

105 lines
2.7 KiB

  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:zhiying_base_widget/pages/main_page/main_page_notifier.dart';
  4. import 'package:zhiying_comm/zhiying_comm.dart';
  5. import 'package:provider/provider.dart';
  6. class MainPage extends StatefulWidget {
  7. MainPage({Key key}) : super(key: key);
  8. @override
  9. _MainPageState createState() => _MainPageState();
  10. }
  11. class _MainPageState extends State<MainPage> {
  12. @override
  13. Widget build(BuildContext context) {
  14. return Scaffold(
  15. backgroundColor: Color(0xfff9f9f9),
  16. body: ChangeNotifierProvider(
  17. create: (context) => MainPageNotifier(),
  18. child: MainPageContainer(),
  19. ),
  20. );
  21. }
  22. }
  23. class MainPageContainer extends StatefulWidget {
  24. MainPageContainer({Key key}) : super(key: key);
  25. @override
  26. _MainPageContainerState createState() => _MainPageContainerState();
  27. }
  28. class _MainPageContainerState extends State<MainPageContainer> {
  29. WidgetType _type = WidgetType.normal;
  30. bool _isEnded = false;
  31. ScrollController _controller = ScrollController();
  32. @override
  33. void dispose() {
  34. _controller.dispose();
  35. super.dispose();
  36. }
  37. @override
  38. void initState() {
  39. _reload();
  40. _controller.addListener(() {
  41. // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
  42. if (_controller.offset >= _controller.position.maxScrollExtent &&
  43. !_isEnded) {
  44. // 滑动到底部
  45. _isEnded = true;
  46. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  47. } else if (_controller.offset < _controller.position.maxScrollExtent &&
  48. _isEnded) {
  49. _isEnded = false;
  50. Provider.of<MainPageNotifier>(context, listen: false).reset();
  51. }
  52. });
  53. super.initState();
  54. }
  55. void _reload() {
  56. setState(() {
  57. _type = WidgetType.skeleton;
  58. });
  59. Future.delayed(Duration(seconds: 3), () {
  60. setState(() {
  61. _type = WidgetType.normal;
  62. });
  63. });
  64. }
  65. List<Widget> _createContent(BuildContext context) {
  66. List<Widget> list = List();
  67. list.add(SliverToBoxAdapter(
  68. child: RaisedButton(
  69. onPressed: () {
  70. Provider.of<MainPageNotifier>(context, listen: false).loadMore();
  71. _reload();
  72. },
  73. child: Text('重置'),
  74. ),
  75. ));
  76. list.addAll(WidgetFactory.create('index_carousel',
  77. type: _type,
  78. isSliver: true,
  79. model: Map<String, dynamic>.from({'aaaaa': 'bbbb'})));
  80. list.addAll(WidgetFactory.create('index_recommend_list',
  81. type: _type, isSliver: true));
  82. return list;
  83. }
  84. @override
  85. Widget build(BuildContext context) {
  86. return SafeArea(
  87. child: CustomScrollView(
  88. controller: _controller,
  89. slivers: _createContent(context),
  90. ),
  91. );
  92. }
  93. }