从旋转木马滑块获取索引-颤振



我在我的代码中使用carousel滑块包,我试图从这个滑块中提取一个索引,以添加到滑块底部的点指示器:我使用两个软件包:https://pub.dev/packages/carousel_slider和Dot 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,
),
),

最新更新