import 'dart:convert'; import 'dart:ui'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import 'package:pull_to_refresh/pull_to_refresh.dart'; import 'package:zhiying_base_widget/pages/main_page/main_page_bloc.dart'; import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart'; import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart'; import 'package:zhiying_base_widget/widgets/base_state/base_state.dart'; import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart'; import 'package:zhiying_base_widget/widgets/mine/mine_nav/mine_nav_bg.dart'; import 'package:zhiying_base_widget/widgets/others/mine_header_bg_widget.dart'; import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_gif_header.dart'; import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_header.dart'; import 'package:zhiying_comm/util/base_bloc.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; class MainPage extends StatefulWidget { final Map data; MainPage( this.data, { Key key, }) : super(key: key); @override _MainPageState createState() => _MainPageState(); } class _MainPageState extends State { @override Widget build(BuildContext context) { return MultiProvider( providers: [ ChangeNotifierProvider.value(value: MainPageNotifier()), ChangeNotifierProvider.value(value: MainPageBgNotifier()), ], child: BlocProvider( bloc: MainPageBloc(), child: _MainPageContainer(widget.data), ), ); } } class _MainPageContainer extends StatefulWidget { final Map data; _MainPageContainer(this.data, {Key key}) : super(key: key); @override _MainPageContainerState createState() => _MainPageContainerState(); } class _MainPageContainerState extends BasePageState<_MainPageContainer> { bool _isEnded = false; ScrollController _controller = ScrollController(); MainPageBloc _bloc; RefreshController _refreshController = RefreshController(initialRefresh: false); Widget _floatWidget; void _onLoading() async { // await Future.delayed(Duration(milliseconds: 1000)); // if (mounted) setState(() {}); // _refreshController.loadComplete(); } void _onRefresh() async { // await Future.delayed(Duration(milliseconds: 3000)); // _refreshController.refreshCompleted(); if (widget.data.containsKey('skip_identifier')) { _bloc.loadData(widget.data['skip_identifier']); refreshPage(); } } @override void dispose() { _controller.dispose(); _refreshController.dispose(); super.dispose(); } @override void initState() { _bloc = BlocProvider.of(context); if (widget.data.containsKey('skip_identifier')) { _bloc.loadData(widget.data['skip_identifier']); } _controller.addListener(() { // print('${_controller.offset} ${_controller.position.maxScrollExtent}'); if (_controller.offset >= _controller.position.maxScrollExtent && !_isEnded) { // 滑动到底部 _isEnded = true; Provider.of(context, listen: false).loadMore(); } else if (_controller.offset < _controller.position.maxScrollExtent && _isEnded) { _isEnded = false; Provider.of(context, listen: false).reset(); } }); super.initState(); } @override Widget buildX(BuildContext context) { double top = MediaQueryData.fromWindow(window).padding.top; return StreamBuilder>>( stream: _bloc.outData, builder: (BuildContext context, AsyncSnapshot snapshot) { print("mainPageBuild"); List widgets = _createContent(context, snapshot.data ?? []); _refreshController.refreshCompleted(); Widget bgWidget = validateBgWidget(context, snapshot.data ?? []); return Scaffold( backgroundColor: HexColor.fromHex("#fff9f9f9"), floatingActionButton: _floatWidget, floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked, body: MediaQuery.removePadding( removeTop: true, context: context, child: Container( width: double.infinity, child: Stack( fit: StackFit.passthrough, children: [ MineHeaderBgWidget( bgWidget: bgWidget, controller: _controller, ), SmartRefresher( enablePullDown: true, enablePullUp: false, header: RefreshGifHeader(), // RefreshHeader( // offsetY: top, // ), controller: _refreshController, onLoading: _onLoading, onRefresh: _onRefresh, child: CustomScrollView( controller: _controller, slivers: widgets, ), ) ], ), ), ), ); }); } List _createContent(BuildContext context, List> datas) { List list = List(); for (int i = 0; i < datas.length; i++) { WidgetModel item = WidgetModel.fromJson(Map.from(datas[i])); print('item.modName ${item.modName}'); if (item.modName == 'index_taobao_auth_tip') { _floatWidget = HomeAuth(datas[i]); continue; } list.addAll(WidgetFactory.create( item.modName, isSliver: true, model: datas[i], )); } if (list.length <= 0) { list.add(SliverToBoxAdapter( child: Container( height: 200, // child: Center( // child: Text('暂时无数据哦~'), // ), ), )); } return list; } ///处理特殊背景图 Widget validateBgWidget(BuildContext context, List> datas) { for (int i = 0; i < datas.length; i++) { WidgetModel item = WidgetModel.fromJson(Map.from(datas[i])); if (item.modName == "profile_background") { final double statusBarHeight = MediaQuery.of(context).padding.top; String url = json.decode(item.data)['img']; return Container( width: double.infinity, height: statusBarHeight + 250, child: CachedNetworkImage( imageUrl: url, fit: BoxFit.fitHeight, ), ); } // if (item.modName == "member_info") { // final double statusBarHeight = MediaQuery.of(context).padding.top; // String url = json.decode(item.data)['bg_image']; // String bgColor = json.decode(item.data)['bg_color']; // return Container( // width: double.infinity, // height: statusBarHeight + 250, // color: HexColor.fromHex(bgColor ?? ""), // child: CachedNetworkImage( // imageUrl: url ?? "", // fit: BoxFit.fitHeight, // ), // ); // } } if (_bloc.backgroundModel != null) { var headerBg = _bloc.backgroundModel.headerBg; return Container( height: double.tryParse(_bloc?.backgroundModel?.headerBg?.height) ?? 0, decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [HexColor.fromHex(headerBg?.mainColor ?? ""), HexColor.fromHex(headerBg?.assistColor ?? ""), HexColor.fromHex(headerBg?.minorColor ?? "")])), ); } return null; } }