From 3b1000b313a5ef82728b7b80181171f02b363e14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=94=BF?= Date: Tue, 27 Aug 2019 21:11:36 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20HomeBanner=20=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E6=9C=80=E5=90=8E=E4=B8=80=E9=A1=B5=E6=BB=91=E5=8A=A8?= =?UTF-8?q?=E8=BF=87=E6=B8=A1=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/components/home_banner.dart | 54 ++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/lib/components/home_banner.dart b/lib/components/home_banner.dart index 36345cbe..1dd45d67 100644 --- a/lib/components/home_banner.dart +++ b/lib/components/home_banner.dart @@ -22,6 +22,7 @@ class _BannerState extends State { int realIndex = 1; PageController controller; Timer timer; + int lastPage=1; @override void initState() { @@ -33,6 +34,27 @@ class _BannerState extends State { duration: Duration(milliseconds: 300), curve: Curves.linear); }); + + controller.addListener(() { + var res = (controller.page - lastPage).abs(); +// setState(() {}); + int count = widget.bannerStories.length; + + if (res > (0.95)) { + if (realIndex == 0) { + controller.jumpToPage( widget.bannerStories.length); + virtualIndex = count - 1; + } else if (realIndex == widget.bannerStories.length + 1) { + virtualIndex = 0; + controller.jumpToPage(1); + } else { + virtualIndex = realIndex - 1; + } + + setState(() { + }); + } + }); } @override @@ -67,9 +89,9 @@ class _BannerState extends State { children: _buildItems(),), _buildIndicator(), // 下面的小点 Positioned(//方法二 - top: 0.0, - right: 0.0, - child: _numberIndicator(context,virtualIndex,widget.bannerStories.length), + top: 0.0, + right: 0.0, + child: _numberIndicator(context,virtualIndex,widget.bannerStories.length), ) ]), ); @@ -102,7 +124,7 @@ class _BannerState extends State { child: Stack( fit: StackFit.expand, children: [ - Image.network(story.image, fit: BoxFit.cover), + Image.network(story.image, fit: BoxFit.cover), _buildItemTitle(story.title), // 内容文字,大意 ],),); } @@ -141,18 +163,20 @@ class _BannerState extends State { } _onPageChanged(int index) { + lastPage = realIndex; + realIndex = index; - int count = widget.bannerStories.length; - if (index == 0) { - virtualIndex = count - 1; - controller.jumpToPage(count); - } else if (index == count + 1) { - virtualIndex = 0; - controller.jumpToPage(1); - } else { - virtualIndex = index - 1; - } - setState(() {}); +// int count = widget.bannerStories.length; +// if (index == 0) { +// virtualIndex = count - 1; +// controller.jumpToPage(count); +// } else if (index == count + 1) { +// virtualIndex = 0; +// controller.jumpToPage(1); +// } else { +// virtualIndex = index - 1; +// } +// setState(() {}); } }