|
- 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_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: SafeArea(
- child: 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> {
- bool _isEnded = false;
- ScrollController _controller = ScrollController();
- MainPageBloc _bloc;
- RefreshController _refreshController =
- RefreshController(initialRefresh: false);
-
- void _onLoading() async {
- // await Future.delayed(Duration(milliseconds: 1000));
- // if (mounted) setState(() {});
- // _refreshController.loadComplete();
- }
-
- @override
- void dispose() {
- _controller.dispose();
- super.dispose();
- }
-
- @override
- void initState() {
- _bloc = BlocProvider.of<MainPageBloc>(context);
- _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();
- }
-
- @override
- Widget build(BuildContext context) {
- return StreamBuilder<Map<String, dynamic>>(
- stream: _bloc.outData,
- builder: (BuildContext context, AsyncSnapshot snapshot) {
- return SmartRefresher(
- enablePullDown: true,
- enablePullUp: false,
- header: WaterDropHeader(),
- controller: _refreshController,
- onLoading: _onLoading,
- child: CustomScrollView(
- controller: _controller,
- slivers: _createContent(context),
- ),
- );
- },
- );
- }
-
- List<Widget> _createContent(BuildContext context) {
- WidgetModel model = WidgetModel.fromJson(widget.data);
-
- List<Widget> list = List();
- for (int i = 0; i < model.components.length; i++) {
- WidgetModel item = model.components[i];
- Map<String, dynamic> data = null;
- if (widget.data.containsKey('components')) {
- data = widget.data['components'][i];
- }
-
- list.addAll(WidgetFactory.create(
- item.modName,
- type: WidgetType.skeleton,
- isSliver: true,
- model: data,
- ));
- }
- model.components.forEach((item) {});
- return list;
- }
- }
|