如何在Flutter中的CarouselSlider.builder中显示列表中图像的颗粒范围



我想显示CarouselSlider.builder中从索引1到3的列表中的图像。

如下所示,我在_imageList中从Api获得了所有数据。

List<ImageModel> _imageList = [];
@override
void initState() {
super.initState();
ApiHelper().getImageList().then((images) {
setState(() {
_imageList = images;
});
});
}

下面是我的图像滑块代码。

CarouselSlider.builder(
itemCount: _imageList.length,
itemBuilder:
(BuildContext context, int index, realindex) {
return Image.network(
"$imageUrl" + _imageList[index].imgPath!,
width: MediaQuery.of(context).size.width - 40,
fit: BoxFit.fill,
);
},
options: CarouselOptions(
viewportFraction: 1,
enlargeCenterPage: true,
initialPage: 1,
),
),

那么我该怎么做呢?非常感谢。

在initState方法中,您可以只创建images列表的子列表,这样您的imageList只包含索引1-3。

_imageList = images.sublist(1, 4);

最新更新