我在我的代碼中使用了 carousel 滑塊包,我正在嘗試從此滑塊中提取索引以添加到滑塊底部的點指示器:我使用了 2 個包:https : //pub.dev/packages /carousel_slider和點指示器
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,
),
),
uj5u.com熱心網友回復:
使用 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,
),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/330992.html
