【问题标题】:I am using Flutter swiper package and I want to achieve the following activity我正在使用 Flutter swiper 包,我想实现以下活动
【发布时间】:2021-01-19 04:28:52
【问题描述】:

我正在使用 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(),
          ),

【问题讨论】:

    标签: flutter dart swiper


    【解决方案1】:

    为 swipper 添加一个控制器 SwiperController controller = SwiperController(); 然后在onTap 称呼 controller.next()

    【讨论】:

      【解决方案2】:

      你必须定义控制器并将其添加到swipper之后,你才能在onTap事件上控制swipper。例如

      final controller=SwiperController()
      

      然后将其分配给 Swipper

      Swiper(
          controller: SwiperController(),
          //Rest are the same...
      )
      

      现在你可以使用控制器来控制swiper的索引,并通过调用方法改变状态例如

      controller.move(1);
      controller.next();
      controller.previous();
      
      
      onTap(){
          controller.next();
          //call the method
      }
      

      【讨论】:

        猜你喜欢
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        • 2019-05-30
        • 1970-01-01
        • 1970-01-01
        • 2023-01-25
        • 2022-01-23
        • 2021-12-22
        相关资源
        最近更新 更多