Flutter 轮播图 flutter_swiper属性说明使用

今天分享的内容是关于图片轮播的实现,使用到的库是flutter_swiper,如果有出现空检查报错的,可以使用flutter_swiper_null_safety

轮播图效果如下:

 

先贴出基本参数详解:

参数说明
itemBuilder列表的构造
indicatorLayout指示器的类型,如带颜色的、可以放大的等等
itemCount轮播列表的数量
autoplay是否自动轮播
layout构建轮播的布局
autoplayDelay自动轮播之间的间隔
duration轮播动画时长
onIndexChanged轮播切换的监听
index初始位置
onTap轮播的点击事件
control左右箭头
loop是否循环轮播
scrollDirection轮播方向
pagination指示器样式
customLayoutOption动画效果
viewportFraction当前视窗比例,小于1时就会在屏幕内,可以看见旁边的轮播图
itemHeight单个轮播的高度
itemWidth单个轮播的宽度
scale轮播图之间的间距

(一)添加依赖并引入

添加依赖:

flutter_swiper: ^1.1.6

  flutter_swiper_null_safety: ^1.0.2

(二)普通的横向/竖向轮播,效果如开头gif中的第一种第三种

代码如下:

Swiper(
                  itemCount: _imageList.length,
                  itemBuilder: (context, index) {
                    return Image.network(
                      _imageList[index],
                      fit: BoxFit.cover,
                    );
                  },
                  autoplay: true,
                  //自动轮播
                  onIndexChanged: (index) {},
                  //引起下标变化的监听
                  onTap: (index) {},
                  //点击轮播时调用
                  duration: 1000,
                  //切换时的动画时间
                  autoplayDelay: 2000,
                  //自动播放间隔毫秒数.
                  autoplayDisableOnInteraction: false,
                  loop: true,
                  //是否无限轮播
                  scrollDirection: Axis.horizontal,
                  //滚动方向
                  index: 0,
                  //初始下标位置
                  scale: 0.6,
                  //轮播图之间的间距
                  viewportFraction: 0.8,
                  //当前视窗比例,小于1时就会在屏幕内,可以看见旁边的轮播图
                  indicatorLayout: PageIndicatorLayout.COLOR,
                  pagination: new SwiperPagination(),
                  //底部指示器
                  control: new SwiperControl(), //左右箭头
                )

换成竖直轮播的代码如下:

//滚动方向
   scrollDirection: Axis.vertical

以上效果会有缩放的样式,如果想要普通的不进行缩放的,可以将以下参数删除:

 scale: 0.6,
                  //轮播图之间的间距
                  viewportFraction: 0.8,
                  //当前视窗比例,小于1时就会在屏幕内,可以看见旁边的轮播

(三)效果图中的第二种

使用下方的参数实现效果:

 layout: SwiperLayout.TINDER

其他的参数和上面的情况一致

(四)效果图中的第四种,带有自定义动画的

代码如下:

Swiper(
                  itemWidth: 300,
                  itemHeight: 200,
                  layout: SwiperLayout.CUSTOM,
                  customLayoutOption: new CustomLayoutOption(
                          startIndex: -1, stateCount: 3)
                      .addRotate([-45.0 / 180, 0.0, 45.0 / 180]).addTranslate([
                    new Offset(-370.0, -40.0),
                    new Offset(0.0, 0.0),
                    new Offset(370.0, -40.0)
                  ]),
                  autoplay: true,
                  duration: 2000,
                  itemBuilder: (context, index) {
                    return new Container(
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(10)),
                          image: DecorationImage(
                              image: NetworkImage(_imageList[index]),
                              fit: BoxFit.fill)),
                    );
                  },
                  itemCount: _imageList.length,
                  indicatorLayout: PageIndicatorLayout.COLOR,
                  pagination: SwiperPagination(
                      alignment: Alignment.bottomCenter,
                      builder: FractionPaginationBuilder(
                          activeColor: Colors.grey,
                          color: Colors.black,
                          fontSize: 20,
                          activeFontSize: 25)),
                  //底部指示器
                )

customLayoutOption用来自定义位移与旋转的动画

(五)自定义指示器

参数说明
pagination定义指示器,对应的是SwiperPagination组件

SwiperPagination组件的参数如下:

参数说明
alignment指示器的位置
margin指示器与父组件的间距
builder指示器的样式,对应的是SwiperPlugin的子类

builder对应的组件主要有以下几种:

  • FractionPaginationBuilder 数字指示器
  builder: FractionPaginationBuilder(
                          activeColor: Colors.grey,
                          color: Colors.black,
                          fontSize: 20,
                          activeFontSize: 25)
  • RectSwiperPaginationBuilder 矩形指示器
 builder: RectSwiperPaginationBuilder(
                          activeColor: Colors.red,
                          color: Colors.grey,
                          size: Size(20, 10),
                          activeSize: Size(20, 10))),
  • DotSwiperPaginationBuilder 圆形指示器
builder: DotSwiperPaginationBuilder(
                  color: Colors.grey,
                  activeColor: Colors.red,
                  size: 10.0,
                  activeSize: 20.0,
                  space: 5.0)

到此为止,使用flutter_swiper实现轮播效果就介绍完了,要是觉得有用记得点个赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A HandSome Man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值