|
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:zhiying_base_widget/pages/main_page/main_page_notifier.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
- import 'package:provider/provider.dart';
-
- class MainPage extends StatefulWidget {
- MainPage({Key key}) : super(key: key);
-
- @override
- _MainPageState createState() => _MainPageState();
- }
-
- class _MainPageState extends State<MainPage> {
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Color(0xfff9f9f9),
- body: ChangeNotifierProvider(
- create: (context) => MainPageNotifier(),
- child: MainPageContainer(),
- ),
- );
- }
- }
-
- class MainPageContainer extends StatefulWidget {
- MainPageContainer({Key key}) : super(key: key);
-
- @override
- _MainPageContainerState createState() => _MainPageContainerState();
- }
-
- class _MainPageContainerState extends State<MainPageContainer> {
- WidgetType _type = WidgetType.normal;
- bool _isEnded = false;
- ScrollController _controller = ScrollController();
-
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
-
- @override
- void initState() {
- _reload();
- _controller.addListener(() {
- // print('${_controller.offset} ${_controller.position.maxScrollExtent}');
- if (_controller.offset >= _controller.position.maxScrollExtent &&
- !_isEnded) {
- // 滑动到底部
- _isEnded = true;
- Provider.of<MainPageNotifier>(context, listen: false).loadMore();
- } else if (_controller.offset < _controller.position.maxScrollExtent &&
- _isEnded) {
- _isEnded = false;
- Provider.of<MainPageNotifier>(context, listen: false).reset();
- }
- });
- super.initState();
- }
-
- void _reload() {
- setState(() {
- _type = WidgetType.skeleton;
- });
- Future.delayed(Duration(seconds: 3), () {
- setState(() {
- _type = WidgetType.normal;
- });
- });
- }
-
- List<Widget> _createContent(BuildContext context) {
- List<Widget> list = List();
- list.add(SliverToBoxAdapter(
- child: RaisedButton(
- onPressed: () {
- Provider.of<MainPageNotifier>(context, listen: false).loadMore();
- _reload();
- },
- child: Text('重置'),
- ),
- ));
- list.addAll(WidgetFactory.create('index_carousel',
- type: _type,
- isSliver: true,
- model: Map<String, dynamic>.from({'aaaaa': 'bbbb'})));
- list.addAll(WidgetFactory.create('index_recommend_list',
- type: _type, isSliver: true));
- return list;
- }
-
- @override
- Widget build(BuildContext context) {
- return SafeArea(
- child: CustomScrollView(
- controller: _controller,
- slivers: _createContent(context),
- ),
- );
- }
- }
|