[英]I am using Flutter swiper package and I want to achieve the following activity
I am Using a Swiper Widget.我正在使用 Swiper 小部件。 I was able to swipe front and back.我能够前后滑动。 But i also want the functionality swiping to the next card while on a onPressed event.但我也希望在 onPressed 事件中刷卡到下一张卡片的功能。 Moreover by pressing a button, I want to move to the next card.此外,通过按一个按钮,我想移动到下一张卡片。 How to achieve this functionality.如何实现这个功能。 This is my Swiper Widget.这是我的 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,
),
)
And this is my button这是我的按钮
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(),
),
add a controller to the swipper SwiperController controller = SwiperController();
将 controller 添加到SwiperController controller = SwiperController();
then on the onTap
call controller.next()
然后在onTap
上调用controller.next()
You have to define and add controller to the swipper after that you can control the swipper on onTap event.such as您必须定义 controller 并将其添加到 swipper 之后,您才能在 onTap 事件上控制 swipper。例如
final controller=SwiperController()
After that assign it to the Swipper as之后将其分配给 Swipper 作为
Swiper(
controller: SwiperController(),
//Rest are the same...
)
Now you can control the index of the swipper using the controller and change the state by calling method such as现在您可以使用 controller 控制 swipper 的索引,并通过调用方法更改 state,例如
controller.move(1);
controller.next();
controller.previous();
onTap(){
controller.next();
//call the method
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.