我已经使用了carousel_slider用于在我的应用程序中向我的用户显示帖子的包。 我想每当用户到达轮播中的最后一个帖子时加载更多帖子。 我在哪里可以触发将更多数据添加到项目列表的函数,以防万一
homeData
这就是我的代码的样子:
@override
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: getMoreData, //fetches new post on refresh
child: CarouselSlider(
options: CarouselOptions(
height: 650,
enableInfiniteScroll: false,
aspectRatio: 2.0,
enlargeCenterPage: true,
scrollDirection: Axis.vertical,
),
items: homeData, //list of widgets that are displayed in the carousel
),
);
}
最佳答案
您可以回调onPageChanged
,每当滑动帖子时都会调用该回调。
具体操作方法如下:
List itemList = [1,2,3,4,5];
CarouselSlider(
options: CarouselOptions(
height: 400.0,
onPageChanged: (index, reason) {
if(index == itemList.length - 1) {
//do whatever you want to do on your last page change
}
},
),
items: itemList.map((i) {
return Builder(
builder: (BuildContext context) {
return Container(
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 5.0),
decoration: BoxDecoration(
color: Colors.amber
),
child: Text('text $i', style: TextStyle(fontSize: 16.0),)
);
},
);
}).toList(),
)
关于flutter - 如何在Flutter中的Carousel Slider末端触发功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65446269/