FLutter Swiper插件踩坑之旅
1.flutter_swiper必须包裹在container中
Container(
child:
AspectRatio(
aspectRatio: 20/9,
child: Swiper(
key: UniqueKey(),
itemBuilder: (BuildContext context, int index) {
return new Image.network(
_Swiperlist[index]["path"].length>0?_Swiperlist[index]["path"]:"https://kt-1301681474.cos.ap-shanghai.myqcloud.com/app/rot/lbt_20200425181036.png",
fit: BoxFit.fill,
);
},
itemCount: _Swiperlist.length,
viewportFraction: 0.8,
scale: 0.9,
),
)
)
2.避免轮播图尺寸在各设备上失真
采用AspectRatio来绑定尺寸
AspectRatio(
aspectRatio: 20/9,
3.接口中获取轮播图引发的一系列错误
Flutter Swiper是一个轮播图组件,内部包含一个Widget List,当这个Widget List数量发生变化的时候如果出现类似这种异常情况导致轮播图不滑动或者其他红屏等错误,
ScrollController not attached to any scroll views.
解决办法
给Swiper加一个LocalKey即可解决,我这里加了个UniqueKey,属于一个LocalKey
AspectRatio(
aspectRatio: 20/9,
child: Swiper(
key: UniqueKey(),
itemBuilder: (BuildContext context, int index) {
return new Image.network(
_Swiperlist[index]["path"].length>0?_Swiperlist[index]["path"]:"https://kt-1301681474.cos.ap-shanghai.myqcloud.com/app/rot/lbt_20200425181036.png",
fit: BoxFit.fill,
);
},
itemCount: _Swiperlist.length,
viewportFraction: 0.8,
scale: 0.9,
),
)