gpt4 book ai didi

flutter - 从轮播 slider 获取索引-Flutter

转载 作者:行者123 更新时间:2023-12-05 03:36:17 25 4
gpt4 key购买 nike

我在我的代码中使用轮播 slider 包,我试图从此 slider 中提取索引以添加到 slider 底部的点指示器中:我正在使用 2 个包:https://pub.dev/packages/carousel_sliderDot Indicator

 Padding(
padding: EdgeInsets.fromLTRB(
size.width * 0.05, 35, size.width * 0.05, 0),
child: CarouselSlider(
options: CarouselOptions(
aspectRatio: 2.0,
enlargeCenterPage: true,
scrollDirection: Axis.horizontal,
autoPlay: true,
),
items: [
Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Image(
image: AssetImage("assets/greenhouse.jpg"),
),
),
Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Image(
image: AssetImage("assets/greenhouse.jpg"),
),
),
],
),
),
new DotsIndicator(
dotsCount: 5,
position: 1,
decorator: DotsDecorator(
color: Colors.grey,
activeColor: greencol,
),
),

最佳答案

使用 onPageChange 保存你的索引:

 int _currentIndex = 0;

Padding(
padding: EdgeInsets.fromLTRB(
size.width * 0.05, 35, size.width * 0.05, 0),
child: CarouselSlider(
options: CarouselOptions(
aspectRatio: 2.0,
enlargeCenterPage: true,
scrollDirection: Axis.horizontal,
autoPlay: true,
onPageChanged: (index, reason) {
_currentIndex = index;
setState((){});
},
),
items: [
Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Image(
image: AssetImage("assets/greenhouse.jpg"),
),
),
Card(
clipBehavior: Clip.antiAlias,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
child: Image(
image: AssetImage("assets/greenhouse.jpg"),
),
),
],
),
),
new DotsIndicator(
dotsCount: 5,
position: _currentIndex.toDouble(),
decorator: DotsDecorator(
color: Colors.grey,
activeColor: greencol,
),
),

关于flutter - 从轮播 slider 获取索引-Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69674515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com