Flutter中实现一个无限轮询的Banner
2021-04-06 本文已影响0人
FlyFighting
在flutter中写banner图的时候,用了第三方库banner时发现有些小问题,所以自己来实现以下。 通过学习和测试,然后直接上代码吧。 原理都和Android中是一样的,只不过是做个记录罢了,方便以后自己使用,顺便给需要的人提供个方便。
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() => runApp(BannerTest());
class BannerTest extends StatefulWidget {
@override
_BannerTestState createState() => _BannerTestState();
}
class _BannerTestState extends State<BannerTest> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Banner"),
),
body: ListView.builder(
itemCount: 10,
itemBuilder: (context, i) {
if (i == 0) {
return Container(
height: 180,
child: BannerView(
children: [
Image.network(
"https://www.wanandroid.com/blogimgs/50c115c2-cf6c-4802-aa7b-a4334de444cd.png",
fit: BoxFit.cover),
Image.network(
"https://www.wanandroid.com/blogimgs/90c6cc12-742e-4c9f-b318-b912f163b8d0.png",
fit: BoxFit.cover),
Image.network(
"https://www.wanandroid.com/blogimgs/ab17e8f9-6b79-450b-8079-0f2287eb6f0f.png",
fit: BoxFit.cover),
Image.network(
"https://www.wanandroid.com/blogimgs/fb0ea461-e00a-482b-814f-4faca5761427.png",
fit: BoxFit.cover),
Image.network(
"https://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png",
fit: BoxFit.cover),
Image.network(
"https://www.wanandroid.com/blogimgs/00f83f1d-3c50-439f-b705-54a49fc3d90d.jpg",
fit: BoxFit.cover),
],
));
} else {
return Text("");
}
}),
),
);
}
}
class BannerView extends StatefulWidget {
final List<Widget> children;
final Duration switchDuration;
BannerView(
{this.children = const <Widget>[],
this.switchDuration = const Duration(seconds: 3)});
@override
_BannerViewState createState() => _BannerViewState();
}
class _BannerViewState extends State<BannerView>
with SingleTickerProviderStateMixin {
TabController _tabController;
PageController _pageController;
Timer _timer;
int _curPageIndex;
static const Duration animateDuration = const Duration(milliseconds: 500);
List<Widget> children = [];
@override
void initState() {
super.initState();
_curPageIndex = 0;
_tabController = TabController(length: widget.children.length, vsync: this);
/// 添加所有的widget到新建的list中
children.addAll(widget.children);
/// 定时器完成自动翻页-只有在大于1时才会有翻页
if (children.length > 1) {
children.insert(0, widget.children.last);
children.add(widget.children.first);
///如果大于一页,则会在前后都加一页, 初始页要是 1
_curPageIndex = 1;
_timer = Timer.periodic(widget.switchDuration, _nextBanner);
}
///初始页面 指定
_pageController = PageController(initialPage: _curPageIndex);
}
/// 进行翻页的动画
_nextBanner(Timer timer) {
_curPageIndex++;
_curPageIndex = _curPageIndex == children.length ? 0 : _curPageIndex;
//curve:和android一样 动画插值
_pageController.animateToPage(_curPageIndex,
duration: animateDuration, curve: Curves.linear);
}
@override
void dispose() {
/// 页面销毁时进行回收
_pageController.dispose();
_tabController.dispose();
_timer?.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
Listener(
/// 点击时取消timer效果
onPointerDown: (_) => _timer?.cancel(),
onPointerUp: (_) {
///重新开启timer
if (children.length > 1) {
_timer = Timer.periodic(widget.switchDuration, _nextBanner);
}
},
child: NotificationListener(
onNotification: (notification){
if(notification is ScrollUpdateNotification) {
ScrollUpdateNotification n = notification;
/// 判断是否是一次完整的翻页
if (n.metrics.atEdge) {
if (_curPageIndex == children.length - 1) {
/// 如果是最后一页,那么就跳到第一页
_pageController.jumpToPage(1);
} else if (_curPageIndex == 0) {
/// 如果是第一页,再往前滑动,因为原来的list前后都加了一条数据,所以 -2
_pageController.jumpToPage(children.length - 2);
}
}
}
},
child: PageView.builder(
itemCount: children.length,
itemBuilder: (context, index) {
/// banner设置点击监听
return InkWell(
child: children[index],
onTap: () {
print("点击Item");
},
);
},
onPageChanged: (index) {
_curPageIndex = index;
if (index == children.length - 1) {
/// 如果是最后一页,那么下面的指示器设置为0的位置
_tabController.animateTo(0);
} else if(index == 0){
///如果是第一页再往左滑,那么久设置为指示器最后的位置
_tabController.animateTo(_tabController.length-1);
}else {
_tabController.animateTo(index-1);
}
},
controller: _pageController,
),
)
),
Positioned(
child: TabPageSelector(
controller: _tabController,
color: Colors.white,
selectedColor: Colors.grey,
),
bottom: 8.0,
right: 8.0,
),
],
);
}
}
可以直接跑起来的,就不做过多讲解了。