|
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart';
- import 'package:zhiying_base_widget/pages/main_page/main_page_notifier.dart';
- import 'package:zhiying_base_widget/pages/main_page/main_page_sk.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
- import 'package:provider/provider.dart';
- import 'package:zhiying_comm/util/base_bloc.dart';
- import 'package:pull_to_refresh/pull_to_refresh.dart';
-
- class MainPage extends StatefulWidget {
- final Map<String, dynamic> data;
-
- MainPage(
- this.data, {
- 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: BlocProvider<MainPageBloc>(
- bloc: MainPageBloc(),
- child: _MainPageContainer(widget.data),
- ),
- ),
- );
- }
- }
-
- class _MainPageContainer extends StatefulWidget {
- final Map<String, dynamic> data;
-
- _MainPageContainer(this.data, {Key key}) : super(key: key);
-
- @override
- _MainPageContainerState createState() => _MainPageContainerState();
- }
-
- class _MainPageContainerState extends State<_MainPageContainer> {
- WidgetType _type = WidgetType.normal;
- bool _isEnded = false;
- ScrollController _controller = ScrollController();
- MainPageBloc _bloc;
-
- List<String> items = ["1", "2", "3", "4", "5", "6", "7", "8"];
- RefreshController _refreshController =
- RefreshController(initialRefresh: false);
-
- void _onRefresh() async {
- // monitor network fetch
- await Future.delayed(Duration(milliseconds: 1000));
- // if failed,use refreshFailed()
- _refreshController.refreshCompleted();
- }
-
- void _onLoading() async {
- // monitor network fetch
- await Future.delayed(Duration(milliseconds: 1000));
- // if failed,use loadFailed(),if no data return,use LoadNodata()
- items.add((items.length + 1).toString());
- if (mounted) setState(() {});
- _refreshController.loadComplete();
- }
-
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
-
- @override
- void initState() {
- _bloc = BlocProvider.of<MainPageBloc>(context);
- _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;
- });
- WidgetModel model = WidgetModel.fromJson(widget.data);
- if ((model?.components?.length ?? 0) > 0) {
- _bloc.loadData(model.components.map((item) => item.modId).toList());
- }
-
- // 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('home_quick_entry', type: _type, isSliver: true));
- list.addAll(WidgetFactory.create('index_recommend_list',
- type: _type, isSliver: true));
- return list;
- }
-
- @override
- Widget build(BuildContext context) {
- return StreamBuilder<List<List<Map<String, dynamic>>>>(
- stream: _bloc.outData,
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- if (snapshot.data == null) {
- return MainPageSkeleton();
- }
-
- _type = WidgetType.normal;
- return SmartRefresher(
- enablePullDown: true,
- enablePullUp: true,
- header: WaterDropHeader(),
- footer: CustomFooter(
- builder: (BuildContext context, LoadStatus mode) {
- Widget body;
- if (mode == LoadStatus.idle) {
- body = Text("上拉加载");
- } else if (mode == LoadStatus.loading) {
- body = CupertinoActivityIndicator();
- } else if (mode == LoadStatus.failed) {
- body = Text("加载失败!点击重试!");
- } else if (mode == LoadStatus.canLoading) {
- body = Text("松手,加载更多!");
- } else {
- body = Text("没有更多数据了!");
- }
- return Container(
- height: 55.0,
- child: Center(child: body),
- );
- },
- ),
- controller: _refreshController,
- onRefresh: _onRefresh,
- onLoading: _onLoading,
- child: CustomScrollView(
- controller: _controller,
- slivers: _createContent(context),
- ),
- );
- },
- );
- }
- }
|