如何解决我正在使用 Flutter swiper 包,我想实现以下活动
我正在使用 Swiper 小部件。我能够前后滑动。但我也希望在 onPressed 事件中滑动到下一张卡片的功能。此外,通过按下按钮,我想移动到下一张卡片。如何实现这个功能。 这是我的 Swiper 小工具。
Swiper(
scrollDirection: Axis.horizontal,itemCount: studentDataWithAttendanceData.isEmpty
? 0
: studentDataWithAttendanceData.length,itemBuilder: (BuildContext context,int index) {
return MyClassRoomCard(
studentData:
studentDataWithAttendanceData[index]
.studentData,attendanceStatus:
studentDataWithAttendanceData[index]
.attendanceStatus,);
},scale: 0.8,viewportFraction: 0.9,onIndexChanged: rollNumberCallback,layout: SwiperLayout.STACK,itemWidth: 375,itemHeight: 500,pagination: SwiperPagination(
builder: DotSwiperPaginationBuilder(
activeColor: Colors.indigo.shade400,color: Colors.grey.shade400,),control: SwiperControl(
color: Colors.indigo.shade400,padding: EdgeInsets.symmetric(horizontal: 15.0),iconPrevious: Icons.arrow_back_ios_rounded,iconNext: Icons.arrow_forward_ios_rounded,size: 20,)
这是我的按钮
RaisedButton(
elevation: 5.0,padding: EdgeInsets.all(22.0),onPressed: onTap,//---------------->by tapping this,I want to change the card to the next.
child: Icon(
icon,color: iconColor,size: 50.0,color: buttonColor,shape: CircleBorder(),
解决方法
为 swipper 添加一个控制器
SwiperController controller = SwiperController();
然后在 onTap
称呼
controller.next()
您必须定义并添加控制器到 swipper 之后,您才能在 onTap 事件上控制 swipper。例如
final controller=SwiperController()
然后将其分配给 Swipper
Swiper(
controller: SwiperController(),//Rest are the same...
)
现在你可以使用控制器控制swipper的索引并通过调用诸如
的方法来改变状态controller.move(1);
controller.next();
controller.previous();
onTap(){
controller.next();
//call the method
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。