|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- import 'dart:async';
-
- import 'package:event_bus/event_bus.dart';
- import 'package:flutter/material.dart';
- import 'package:pull_to_refresh/pull_to_refresh.dart';
- import 'package:provider/provider.dart';
- import 'package:flutter_bloc/flutter_bloc.dart';
- import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_notifier.dart';
- import 'package:zhiying_base_widget/widgets/empty/empty_widget.dart';
- import 'package:zhiying_base_widget/widgets/home/home_auth/home_auth.dart';
- import 'package:zhiying_base_widget/widgets/refresh/refresh_header/refresh_gif_header.dart';
- import 'package:zhiying_comm/zhiying_comm.dart';
- import 'bloc/custom_item_page_bloc.dart';
- import 'bloc/custom_item_page_state.dart';
- import 'bloc/custom_item_page_event.dart';
- import 'bloc/custom_item_page_repository.dart';
- import 'package:zhiying_base_widget/pages/custom_page/event/reload_event.dart';
-
- ///
- /// 通用模块的分类导航下的子模块
- ///
- ///
- class CustomItemPage extends StatelessWidget {
- final Map<String, dynamic> data;
- final int tabIndex;
- final String modPid;
- final String modId;
- final bool needBuildStatus;
- Function(double) scroller;
-
- CustomItemPage(this.data, this.tabIndex, this.modId, this.modPid, this.needBuildStatus, {this.scroller});
-
- @override
- Widget build(BuildContext context) {
- return MultiProvider(
- providers: [
- ChangeNotifierProvider.value(value: MainPageNotifier()),
- ],
- child: BlocProvider<CustomItemPageBloc>(
- create: (_) => CustomItemPageBloc(CustomItemPageRepository(this.data, this.tabIndex, this.modId, this.modPid)),
- child: _CustomItemPageContainer(this.data, this.tabIndex, this.modId, this.modPid, this.needBuildStatus,scroller: this.scroller,),
- ),
- );
- }
- }
-
- class _CustomItemPageContainer extends StatefulWidget {
- final Map<String, dynamic> data;
- final int tabIndex;
- final String modPid;
- final String modId;
- final bool needBuildStatus;
- final Function(double) scroller;
-
- const _CustomItemPageContainer(this.data, this.tabIndex, this.modId, this.modPid, this.needBuildStatus, {this.scroller, Key key}) : super(key: key);
-
- @override
- __CustomItemPageContainerState createState() => __CustomItemPageContainerState();
- }
-
- class __CustomItemPageContainerState extends State<_CustomItemPageContainer> with AutomaticKeepAliveClientMixin {
- @override
- bool get wantKeepAlive => true;
-
- ScrollController _controller;
- RefreshController _refreshController;
- EventBus _eventBus;
-
- /// 回到顶点
- void _scrollTop() {
- // _controller.jumpTo(0);
- _controller.animateTo(0, duration: Duration(milliseconds: 200), curve: Curves.linear);
- }
-
- /// 初始化
- void _initEvent() {
- BlocProvider.of<CustomItemPageBloc>(context).add(CustomItemPageInitEvent());
- }
-
- /// 刷新
- void _refreshEvent() {
- BlocProvider.of<CustomItemPageBloc>(context).add(CustomItemPageRefreshEvent());
- }
-
- /// 下拉更多
- void _loadEvent() {
- // BlocProvider.of<CustomItemPageBloc>(context).add(CustomItemPageLoadEvent());
- Provider.of<MainPageNotifier>(context, listen: false).loadMore();
- Future.delayed(Duration(milliseconds: 500), () => _refreshController?.loadComplete());
- }
-
- @override
- void initState() {
- _controller = ScrollController();
- _eventBus = EventBus();
- _refreshController = RefreshController(initialRefresh: false);
- _initEvent();_controller.addListener(() {
- if(widget.scroller!=null){
- widget?.scroller(_controller.offset);
- }
- });
- super.initState();
- }
-
- @override
- void dispose() {
- _controller?.dispose();
- _refreshController?.dispose();
- _eventBus?.destroy();
- super.dispose();
- }
-
- @override
- Widget build(BuildContext context) {
- return BlocConsumer<CustomItemPageBloc, CustomItemPageState>(
- listener: (context, state) {},
- buildWhen: (prev, current) {
- if (current is CustomItemPageRefreshSuccessState) {
- _refreshController?.refreshCompleted(resetFooterState: true);
- return false;
- }
- if (current is CustomItemPageRefreshErrorState) {
- _refreshController?.refreshFailed();
- return false;
- }
- if (current is CustomItemPageLoadSuccessState) {
- _refreshController?.loadComplete();
- return false;
- }
- if (current is CustomItemPageLoadErrorState) {
- _refreshController?.loadNoData();
- return false;
- }
- if (current is CustomItemPageErrorState) {
- return false;
- }
- if (current is CustomItemPageAppRestartState) {
- // 重启App
- Logger.log('开始重新启动App CustomItemPageAppRestartState');
- EventUtil.instance.fire(ReloadEvent());
- return false;
- }
- return true;
- },
- builder: (context, state) {
- Logger.log('Custom item page builder 刷新了');
- if (state is CustomItemPageLoadedState) {
- Logger.log('custom item page current state = ' + state?.toString());
- if (EmptyUtil.isEmpty(state.model))
- return _buildEmptyWidget();
- else
- return _buildMainWidget(state.model);
- }
- if (state is CustomItemPageInitErrorState) {
- return _buildEmptyWidget();
- }
- return _buildSkeletonWidget();
- },
- );
- }
-
- /// 有数据
- Widget _buildMainWidget(final List<Map<String, dynamic>> model) {
- return MediaQuery.removePadding(
- context: context,
- removeTop: false,
- child: Stack(
- children: <Widget>[
- SmartRefresher(
- controller: _refreshController,
- enablePullDown: true,
- enablePullUp: true,
- onRefresh: _refreshEvent,
- onLoading: _loadEvent,
- header: RefreshGifHeader(),
- // footer: RefreshFooter(),
- child: CustomScrollView(
- physics: BouncingScrollPhysics(),
- controller: _controller,
- slivers: _buildContentWidgets(model),
- ),
- ),
- Align(
- alignment: Alignment.bottomCenter,
- child: Container(
- margin: const EdgeInsets.only(bottom: 8),
- child: _buildAuthWidget(model),
- ), //_buildAuthWidget(model),
- )
- ],
- ),
- );
- }
-
- /// 根据widget的modName生成视图
- List<Widget> _buildContentWidgets(final List<Map<String, dynamic>> datas) {
- List<Widget> result = [];
- for (int i = 0; i < datas.length; i++) {
- WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
- if (item.modName == 'audit_tip') {
- Logger.debug('授权组件,跳过');
- continue;
- }
- if (item.modName == 'product') {
- datas[i]['eventBus'] = _eventBus;
- }
- result.addAll(WidgetFactory.create(
- item.modName,
- isSliver: true,
- model: datas[i],
- ));
- }
-
- // if (widget.needBuildStatus) {
- // double top = MediaQueryData.fromWindow(window).padding.top;
- // result.insert(
- // 0,
- // SliverPersistentHeader(
- // pinned: false,
- // floating: false,
- // delegate: CustomSliverPersistentHeaderDelegate(max: top, min: top, child: Container(
- // // color: Colors.yellow,
- // color: HexColor.fromHex('#FFFF4242'),
- // )),
- // ));
- //
- // }
-
- return result;
- }
-
- /// 空数据
- Widget _buildEmptyWidget() {
- return SmartRefresher(
- controller: _refreshController,
- onRefresh: _refreshEvent,
- enablePullDown: true,
- child: Container(
- child: EmptyWidget(),
- ),
- );
- }
-
- /// 骨架图
- Widget _buildSkeletonWidget() {
- return Container();
- }
-
- /// 特殊的授权组件
- Widget _buildAuthWidget(final List<Map<String, dynamic>> datas) {
- int length = datas?.length ?? 0;
- if (length == 0) return Container();
- Widget rlt;
- for (int i = 0; i < datas.length; i++) {
- WidgetModel item = WidgetModel.fromJson(Map<String, dynamic>.from(datas[i]));
- if (item.modName == 'audit_tip') {
- rlt = HomeAuth(datas[i]);
- break;
- }
- }
-
- return rlt ?? Container();
- }
- }
|