From 35c387c4110dbdd692b3ada12d0bf95304d8c66d Mon Sep 17 00:00:00 2001 From: PH2 <1293456824@qq.com> Date: Tue, 18 May 2021 20:01:31 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81CustomPage=E7=9A=84=E5=88=B7=E6=96=B0?= =?UTF-8?q?=E9=A1=BA=E5=BA=8F=E4=BF=AE=E6=94=B9=202=E3=80=81Custom=20Widge?= =?UTF-8?q?t=E7=BB=84=E4=BB=B6=E7=9A=84=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bloc/custom_item_page_bloc.dart | 2 +- .../bloc/custom_item_page_state.dart | 3 + lib/pages/custom_page/custom_item_page.dart | 16 +- .../bloc/custom_quick_entry_bloc.dart | 4 +- .../bloc/custom_quick_entry_event.dart | 10 +- .../bloc/custom_quick_entry_repository.dart | 16 +- .../bloc/custom_quick_entry_state.dart | 9 +- .../custom/multi_nav/custom_quick_entry.dart | 191 +++++++++--------- .../notice/bloc/custom_notice_bloc.dart | 2 +- .../notice/bloc/custom_notice_repository.dart | 4 +- .../custom/notice/custom_notice_widget.dart | 61 +++--- .../bloc/custom_slide_banner_bloc.dart | 2 +- .../bloc/custom_slide_banner_repository.dart | 2 +- .../slide_banner/custom_slide_banner.dart | 66 ++---- 14 files changed, 193 insertions(+), 195 deletions(-) diff --git a/lib/pages/custom_page/bloc/custom_item_page_bloc.dart b/lib/pages/custom_page/bloc/custom_item_page_bloc.dart index ae17400..661aa28 100644 --- a/lib/pages/custom_page/bloc/custom_item_page_bloc.dart +++ b/lib/pages/custom_page/bloc/custom_item_page_bloc.dart @@ -61,8 +61,8 @@ class CustomItemPageBloc extends Bloc Stream _mapRefreshEventToState(CustomItemPageRefreshEvent event) async* { var netStyle = await repository.fetchNetStyle(); if (!EmptyUtil.isEmpty(netStyle)) { - yield CustomItemPageRefreshSuccessState(); yield CustomItemPageLoadedState(model: netStyle); + yield CustomItemPageRefreshSuccessState(); } else { yield CustomItemPageRefreshErrorState(); yield CustomItemPageErrorState(); diff --git a/lib/pages/custom_page/bloc/custom_item_page_state.dart b/lib/pages/custom_page/bloc/custom_item_page_state.dart index 387731e..f46caa3 100644 --- a/lib/pages/custom_page/bloc/custom_item_page_state.dart +++ b/lib/pages/custom_page/bloc/custom_item_page_state.dart @@ -28,6 +28,9 @@ class CustomItemPageLoadSuccessState extends CustomItemPageState {} /// 上拉更多失败 class CustomItemPageLoadErrorState extends CustomItemPageState {} +/// 没有更多数据的情况 +class CustomItemPageNoMoreDataState extends CustomItemPageState {} + /// 其他错误 class CustomItemPageErrorState extends CustomItemPageState {} diff --git a/lib/pages/custom_page/custom_item_page.dart b/lib/pages/custom_page/custom_item_page.dart index eefb351..cd2a6f5 100644 --- a/lib/pages/custom_page/custom_item_page.dart +++ b/lib/pages/custom_page/custom_item_page.dart @@ -89,10 +89,6 @@ class __CustomItemPageContainerState extends BasePageState<_CustomItemPageContai /// 刷新 void _refreshEvent() { BlocProvider.of(context).add(CustomItemPageRefreshEvent()); - - ///刷新页面 - - refreshPage(); } /// 下拉更多 @@ -129,22 +125,34 @@ class __CustomItemPageContainerState extends BasePageState<_CustomItemPageContai return BlocConsumer( listener: (context, state) {}, buildWhen: (prev, current) { + /// 下拉刷新成功 if (current is CustomItemPageRefreshSuccessState) { _refreshController?.refreshCompleted(resetFooterState: true); + ///刷新页面 + refreshPage(); 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 CustomItemPageNoMoreDataState) { + _refreshController?.loadNoData(); + return false; + } + /// 数据加载出错 if (current is CustomItemPageErrorState) { return false; } diff --git a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_bloc.dart b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_bloc.dart index 9d747f7..577191e 100644 --- a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_bloc.dart +++ b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_bloc.dart @@ -4,6 +4,7 @@ import 'dart:math'; import 'package:bloc/bloc.dart'; import 'package:flutter/cupertino.dart'; import 'package:zhiying_comm/util/empty_util.dart'; +import 'package:zhiying_comm/zhiying_comm.dart'; import './bloc.dart'; @@ -25,8 +26,9 @@ class CustomQuickEntryBloc extends Bloc _mapHomeQuickEntryInitToState(CustomQuickEntryInitEvent event) async* { /// 获取父页面传进来的数据 - var parentData = await repository.fetchPreantData(event: event); + var parentData = repository.fetchParentData(model: event?.model); if (!EmptyUtil.isEmpty(parentData)) { + Logger.log('多言导航 == ' + parentData?.toJson()?.toString()); yield CustomQuickEntryLoadedState(model: parentData); } else { yield CustomQuickEntryErrorState(); diff --git a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_event.dart b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_event.dart index 630b2c7..25eb63d 100644 --- a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_event.dart +++ b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_event.dart @@ -1,10 +1,5 @@ -import 'package:equatable/equatable.dart'; - -abstract class CustomQuickEntryEvent extends Equatable { +abstract class CustomQuickEntryEvent { const CustomQuickEntryEvent(); - - @override - List get props => []; } /// 初始事件 @@ -12,7 +7,4 @@ class CustomQuickEntryInitEvent extends CustomQuickEntryEvent { final Map model; const CustomQuickEntryInitEvent({this.model}); - - @override - List get props => [this.model]; } diff --git a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_repository.dart b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_repository.dart index 1161070..ff98395 100644 --- a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_repository.dart +++ b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_repository.dart @@ -10,11 +10,11 @@ import 'custom_quick_entry_event.dart'; class CustomQuickEntryRepository { /// 获取数据 - Future fetchData({@required CustomQuickEntryInitEvent event}) async { + Future fetchData({@required Map model}) async { try { - var result = await NetUtil.post('/api/v1/mod/${event.model['mod_id']}', + var result = await NetUtil.post('/api/v1/mod/${model['mod_id']}', params: { - 'ids': [event.model['mod_id']] + 'ids': [model['mod_id']] }, cache: true, method: NetMethod.GET); @@ -31,9 +31,9 @@ class CustomQuickEntryRepository { } /// 获取父页面传进来的数据 - Future fetchPreantData({@required CustomQuickEntryInitEvent event}) async { + CustomQuickEntryModel fetchParentData({@required Map model}) { try { - String jsonStr = event.model['data']; + String jsonStr = model['data']; return CustomQuickEntryModel.fromJson(json.decode(jsonStr)); } catch (e, s) { Logger.error(e, s); @@ -42,10 +42,10 @@ class CustomQuickEntryRepository { } /// 获取缓存数据 - Future fetchCachedData({@required CustomQuickEntryInitEvent event}) async { + Future fetchCachedData({@required Map model}) async { try { - var result = await NetUtil.getRequestCachedData('/api/v1/mod/${event.model['mod_id']}', params: { - 'ids': [event.model['mod_id']] + var result = await NetUtil.getRequestCachedData('/api/v1/mod/${model['mod_id']}', params: { + 'ids': [model['mod_id']] }); if (!EmptyUtil.isEmpty(result)) { return _getHomeQuickEntryModel(result); diff --git a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_state.dart b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_state.dart index ff87323..c8fa408 100644 --- a/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_state.dart +++ b/lib/widgets/custom/multi_nav/bloc/custom_quick_entry_state.dart @@ -18,12 +18,13 @@ class InitialCustomQuickEntryState extends CustomQuickEntryState { /// 加载数据完毕 class CustomQuickEntryLoadedState extends CustomQuickEntryState { - final CustomQuickEntryModel model; - - const CustomQuickEntryLoadedState({@required this.model}); + CustomQuickEntryModel model; + CustomQuickEntryLoadedState({@required this.model}); + // @override + // List get props => [this.model]; @override - List get props => [this.model]; + bool operator == (Object other) => false; } /// 加载缓存数据 diff --git a/lib/widgets/custom/multi_nav/custom_quick_entry.dart b/lib/widgets/custom/multi_nav/custom_quick_entry.dart index bca50a2..a8e1469 100644 --- a/lib/widgets/custom/multi_nav/custom_quick_entry.dart +++ b/lib/widgets/custom/multi_nav/custom_quick_entry.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:zhiying_base_widget/widgets/base_state/base_state.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; @@ -11,83 +10,96 @@ import 'custom_quick_entry_sk.dart'; import 'model/custom_quick_entry_model.dart'; import 'bloc/bloc.dart'; -class CustomQuickEntry extends StatelessWidget { +class CustomQuickEntry extends StatefulWidget { final Map model; - const CustomQuickEntry(this.model); + const CustomQuickEntry(this.model, {Key key}) : super(key: key); + + @override + _CustomQuickEntryState createState() => _CustomQuickEntryState(); +} + +class _CustomQuickEntryState extends BaseWidgetState with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { + + + CustomQuickEntryModel model; + CustomQuickEntryRepository _repository; + + @override + bool get wantKeepAlive => true; + + @override + void refreshWidget(String value) { + setState(() { + Logger.log('多言导航 ===== ' + widget?.model?.toString()); + model = _repository.fetchParentData(model: widget?.model); + }); + } + + @override + void initState() { + _repository = CustomQuickEntryRepository(); + model = _repository.fetchParentData(model: widget?.model); + super.initState(); + } + + @override + void dispose() { + model = null; + _repository = null; + super.dispose(); + } @override Widget build(BuildContext context) { - return BlocProvider( - create: (_) { - return CustomQuickEntryBloc(repository: CustomQuickEntryRepository())..add(CustomQuickEntryInitEvent(model: model)); - }, - child: _CustomQuickEntryContainer(model), - ); + /// 空视图 + if (EmptyUtil.isEmpty(model)) { + return CustomQuickEntrySkeleton(); + } + if (model.isShowCategory == "1") { // 有分类 + return CustomQuickCateEntry(model: model); + } else { // 无分类 + return CustomQuickNormalEntry(model: model); + } } -} -class _CustomQuickEntryContainer extends StatefulWidget { - final Map model; +} - _CustomQuickEntryContainer(this.model, {Key key}) : super(key: key); +/// +/// 无分类多眼导航 +/// +class CustomQuickNormalEntry extends StatefulWidget { + CustomQuickEntryModel model; + CustomQuickNormalEntry({this.model}); @override - __CustomQuickEntryContainerState createState() => __CustomQuickEntryContainerState(); + _CustomQuickNormalEntryState createState() => _CustomQuickNormalEntryState(); } -class __CustomQuickEntryContainerState extends BaseWidgetState<_CustomQuickEntryContainer> with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { +class _CustomQuickNormalEntryState extends State with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { + SwiperController _controller; + /// Icon点击事件 void _itemIconClick(ListStyle model) { print("item type = ${model.skipIdentifier}"); - // Navigator.push(context, CupertinoPageRoute(builder: (_) => CommonPage(null))); RouterUtil.route(model, model.toJson(), context); } - SwiperController _controller; - @override void initState() { _controller = SwiperController(); - print(widget?.model); super.initState(); } @override void dispose() { _controller?.dispose(); - super.dispose(); } @override Widget build(BuildContext context) { - return BlocConsumer( - listener: (context, state) {}, - buildWhen: (prev, current) { - if (prev is CustomQuickEntryLoadedState) { - return false; - } - return true; - }, - builder: (context, state) { - print("构建"); - if (state is CustomQuickEntryCachedState) { - return _getMainWidget(state.model); - } - if (state is CustomQuickEntryLoadedState) { - if (state.model.isShowCategory == "1") { - return CustomQuickCateEntry(model: state.model); - } else { - return _getMainWidget(state.model); - } - } - if (state is CustomQuickEntryErrorState) { - return Container(); - } - return CustomQuickEntrySkeleton(); - }, - ); + return _getMainWidget(widget?.model); } Widget _getMainWidget(CustomQuickEntryModel model) { @@ -261,15 +273,15 @@ class __CustomQuickEntryContainerState extends BaseWidgetState<_CustomQuickEntry /// 行的数据 Widget _getRowWidget( {double titleHeight, - double iconHeight, - int totalPage, - int currentPage, - int columSize, - int totalRowSize, - int totalDataSize, - CustomQuickEntryModel model, - int currentRow, - double itemHeight}) { + double iconHeight, + int totalPage, + int currentPage, + int columSize, + int totalRowSize, + int totalDataSize, + CustomQuickEntryModel model, + int currentRow, + double itemHeight}) { List itemList = []; for (int i = 0; i < columSize; i++) { itemList.add(i); @@ -299,16 +311,16 @@ class __CustomQuickEntryContainerState extends BaseWidgetState<_CustomQuickEntry /// item 的数据 Widget _getColumWidget( {double titleHeight, - double iconHeight, - int totalPage, - int currentPage, - int columSize, - int totalRowSize, - int totalDataSize, - CustomQuickEntryModel model, - int currentRow, - int currentColum, - double itemHeight}) { + double iconHeight, + int totalPage, + int currentPage, + int columSize, + int totalRowSize, + int totalDataSize, + CustomQuickEntryModel model, + int currentRow, + int currentColum, + double itemHeight}) { // 当前index = 当前的页数+1 * 当前的行数 + 当前的列数 // int currentIndex = (currentPage + 1) * currentRow + currentColum + currentRow*columSize; // int currentIndex = currentPage != 0 ? currentPage * (columSize * totalRowSize) + columSize + currentRow * columSize : @@ -447,33 +459,15 @@ class __CustomQuickEntryContainerState extends BaseWidgetState<_CustomQuickEntry }); } - @override - // TODO: implement wantKeepAlive - bool get wantKeepAlive => true; @override - void refreshWidget(String value) { - setState(() {}); - } + bool get wantKeepAlive => true; } + /// -/// 图片build 优化 +/// 有分类多眼导航 /// -class MyNetWorkImage extends StatelessWidget { - final String imgUrl; - final double width; - - const MyNetWorkImage(this.imgUrl, {this.width = 40}); - - @override - Widget build(BuildContext context) { - return RepaintBoundary( - child: CachedNetworkImage(width: width, imageUrl: imgUrl), - ); - } -} - class CustomQuickCateEntry extends StatefulWidget { final CustomQuickEntryModel model; @@ -483,7 +477,8 @@ class CustomQuickCateEntry extends StatefulWidget { _CustomQuickCateEntryState createState() => _CustomQuickCateEntryState(); } -class _CustomQuickCateEntryState extends BaseWidgetState with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { +class _CustomQuickCateEntryState extends State with TickerProviderStateMixin, AutomaticKeepAliveClientMixin { + TabController tabController; bool isOnTap = false; @@ -980,11 +975,6 @@ class _CustomQuickCateEntryState extends BaseWidgetState w @override bool get wantKeepAlive => true; - - @override - void refreshWidget(String value) { - BlocProvider.of(context).add(CustomQuickEntryInitEvent(model: widget?.model?.toJson())); - } } class PageItem { @@ -994,3 +984,20 @@ class PageItem { List listStyle; } + +/// +/// 图片build 优化 +/// +class MyNetWorkImage extends StatelessWidget { + final String imgUrl; + final double width; + + const MyNetWorkImage(this.imgUrl, {this.width = 40}); + + @override + Widget build(BuildContext context) { + return RepaintBoundary( + child: CachedNetworkImage(width: width, imageUrl: imgUrl), + ); + } +} \ No newline at end of file diff --git a/lib/widgets/custom/notice/bloc/custom_notice_bloc.dart b/lib/widgets/custom/notice/bloc/custom_notice_bloc.dart index 1ab6e7a..b493dcc 100644 --- a/lib/widgets/custom/notice/bloc/custom_notice_bloc.dart +++ b/lib/widgets/custom/notice/bloc/custom_notice_bloc.dart @@ -21,7 +21,7 @@ class CustomNoticeBloc extends Bloc { /// 初始化 Stream _mapInitEventToState(CustomNoticeInitEvent event) async* { - var parentData = await repository.fetchParentData(event); + var parentData = repository.fetchParentData(event?.model); if(!EmptyUtil.isEmpty(parentData)) yield CustomNoticeLoadedState(model: parentData); else diff --git a/lib/widgets/custom/notice/bloc/custom_notice_repository.dart b/lib/widgets/custom/notice/bloc/custom_notice_repository.dart index 395900c..250cb9b 100644 --- a/lib/widgets/custom/notice/bloc/custom_notice_repository.dart +++ b/lib/widgets/custom/notice/bloc/custom_notice_repository.dart @@ -4,9 +4,9 @@ import 'bloc.dart'; class CustomNoticeRepository { /// 获取父页面的数据 - Future fetchParentData(CustomNoticeInitEvent event) async { + CustomNoticeModel fetchParentData(final Map model) { try { - String jsonStr = event.model['data']; + String jsonStr = model['data']; return CustomNoticeModel.fromJson(jsonDecode(jsonStr)); } catch (e) { Logger.log(e); diff --git a/lib/widgets/custom/notice/custom_notice_widget.dart b/lib/widgets/custom/notice/custom_notice_widget.dart index 140acc1..ee73261 100644 --- a/lib/widgets/custom/notice/custom_notice_widget.dart +++ b/lib/widgets/custom/notice/custom_notice_widget.dart @@ -6,32 +6,24 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:zhiying_comm/zhiying_comm.dart'; import 'bloc/bloc.dart'; import 'custom_notice_sk.dart'; +import 'package:zhiying_base_widget/widgets/base_state/base_state.dart'; /// /// 公告滚动widget /// -class CustomNoticeWidget extends StatelessWidget { +class CustomNoticeWidget extends StatefulWidget { final Map model; - const CustomNoticeWidget(this.model); @override - Widget build(BuildContext context) { - return BlocProvider( - create: (_) => - CustomNoticeBloc(repository: CustomNoticeRepository()) - ..add(CustomNoticeInitEvent(model: model)), - child: _CustomNoticeWidgetContainer(), - ); - } + _CustomNoticeWidgetState createState() => _CustomNoticeWidgetState(); } -class _CustomNoticeWidgetContainer extends StatefulWidget { - @override - _CustomNoticeWidgetContainerState createState() => _CustomNoticeWidgetContainerState(); -} +class _CustomNoticeWidgetState extends BaseWidgetState { + + CustomNoticeModel model; + CustomNoticeRepository _repository; -class _CustomNoticeWidgetContainerState extends State<_CustomNoticeWidgetContainer> { /// 子item点击事件 void _itemOnClick(CustomNoticeModel model) { if (pageIndex == model.listStyle.length) { @@ -48,20 +40,33 @@ class _CustomNoticeWidgetContainerState extends State<_CustomNoticeWidgetContain RouterUtil.route(model, model.toJson(), context); } + @override + void refreshWidget(String value) { + setState(() { + model = _repository.fetchParentData(widget?.model); + }); + } + + @override + void initState() { + _repository = new CustomNoticeRepository(); + model = _repository.fetchParentData(widget?.model); + super.initState(); + } + + @override + void dispose() { + model = null; + _repository = null; + super.dispose(); + } + @override Widget build(BuildContext context) { - return BlocConsumer( - listener: (context, state) {}, - buildWhen: (prev, current) { - return true; - }, - builder: (context, state) { - if (state is CustomNoticeLoadedState) { - return _getMainWidget(state?.model); - } - return CustomNoticeSkeleton(); - }, - ); + if (!EmptyUtil.isEmpty(model)) + return _getMainWidget(model); + else + return CustomNoticeSkeleton(); } /// 主体页面 @@ -138,8 +143,10 @@ class _CustomNoticeWidgetContainerState extends State<_CustomNoticeWidgetContain ), ); } + } + // 上下滚动的消息轮播 class MarqueeWidget extends StatefulWidget { int count; // 子视图数量 diff --git a/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_bloc.dart b/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_bloc.dart index aace5a7..ba2a7e1 100644 --- a/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_bloc.dart +++ b/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_bloc.dart @@ -26,7 +26,7 @@ class CustomSlideBannerBloc extends Bloc _mapInitEventToState(CustomBannerInitEvent event) async* { - var parent = await repository.fetchPreantData(event.model); + var parent = repository.fetchParentData(event.model); if (!EmptyUtil.isEmpty(parent)) { yield HomeSlideBannerLoadedState(datas: parent); return; diff --git a/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_repository.dart b/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_repository.dart index 07d57bb..bd5ff25 100644 --- a/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_repository.dart +++ b/lib/widgets/custom/slide_banner/bloc/custom_slide_banner_repository.dart @@ -19,7 +19,7 @@ class CustomSlideBannerRepository { } /// 获取父页面传进来的数据 - Future fetchPreantData(@required Map model) async { + CustomSlideBannerModel fetchParentData(@required Map model) { try { if (!EmptyUtil.isEmpty(model)) { return CustomSlideBannerModel.fromJson(jsonDecode(model['data'])); diff --git a/lib/widgets/custom/slide_banner/custom_slide_banner.dart b/lib/widgets/custom/slide_banner/custom_slide_banner.dart index edd097e..3671bb6 100644 --- a/lib/widgets/custom/slide_banner/custom_slide_banner.dart +++ b/lib/widgets/custom/slide_banner/custom_slide_banner.dart @@ -1,7 +1,6 @@ import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_swiper/flutter_swiper.dart'; import 'package:provider/provider.dart'; import 'package:zhiying_base_widget/pages/main_page/notifier/main_page_bg_notifier.dart'; @@ -10,6 +9,7 @@ import 'bloc/bloc.dart'; import 'bloc/custom_slide_banner_repository.dart'; import 'custom_slide_banner_sk.dart'; import 'model/custom_slide_banner_model.dart'; +import 'package:zhiying_base_widget/widgets/base_state/base_state.dart'; /// /// 可滑动 banner 轮播图 @@ -23,71 +23,48 @@ class CustomSlideBanner extends StatefulWidget { _CustomSlideBannerState createState() => _CustomSlideBannerState(); } -class _CustomSlideBannerState extends State { - @override - Widget build(BuildContext context) { - return BlocProvider( - create: (_) => CustomSlideBannerBloc(repository: CustomSlideBannerRepository())..add(CustomBannerInitEvent(model: widget?.model)), - child: CustomSlideBannerContainer(), - ); - } -} +class _CustomSlideBannerState extends BaseWidgetState { -class CustomSlideBannerContainer extends StatefulWidget { - @override - _CustomSlideBannerContainerState createState() => _CustomSlideBannerContainerState(); -} + CustomSlideBannerRepository _repository; + CustomSlideBannerModel model; + SwiperController _swiperController; -class _CustomSlideBannerContainerState extends State { /// 子元素点击事件 void _itemOnClick(IndexCarouselList model) { print('点击了 $model'); RouterUtil.route(model, model.toJson(), context); - // Navigator.push(context, CupertinoPageRoute(builder: (_) => VipCenterPage(null))); } - SwiperController _swiperController; - @override void initState() { _swiperController = SwiperController(); + _repository = CustomSlideBannerRepository(); + model = _repository.fetchParentData(widget?.model); super.initState(); } + @override + void refreshWidget(String value) { + setState(() { + model = _repository.fetchParentData(widget?.model); + }); + } + @override void dispose() { - _swiperController.stopAutoplay(); + _swiperController?.stopAutoplay(); _swiperController?.dispose(); + _repository = null; + model = null; super.dispose(); } @override Widget build(BuildContext context) { - return BlocConsumer( - listener: (BuildContext context, CustomSlideBannerState state) { - if (state is HomeSlideBannerLoadError) { - print('数据加载出错'); - } - }, - buildWhen: (previous, current) { - /// 数据加载出错不进行build - if (current is HomeSlideBannerLoadError) { - return false; - } - return true; - }, - builder: (context, state) { - print('currente state = $state'); - if (state is HomeSlideBannerLoadedState) { - return _getMainWidget(state.datas); - } - if (state is CustomSlideBannerCachedState) { - return _getMainWidget(state.datas); - } - // 骨架屏 - return CustomSlideBannerSkeleton(); - }, - ); + if (!EmptyUtil.isEmpty(model)) + return _getMainWidget(model); + else + return CustomSlideBannerSkeleton(); } Widget _getMainWidget(CustomSlideBannerModel datas) { @@ -260,3 +237,4 @@ class _CustomSlideBannerContainerState extends State margin: const EdgeInsets.only(), builder: DotSwiperPaginationBuilder(color: HexColor.fromHex(unselectColor), activeColor: HexColor.fromHex(selectColor), size: 8, activeSize: 8)); } } +