首页 > 解决方案 > 按下图像时如何暂停carousel_pro的自动播放?

问题描述

实际上,carousel_slider 具有完美运行的 pauseAutoPlayOnTouch,但我正在使用 Stateless Widget 组件进入 Stateful Widget。所以,我试图暂停使用 carousel_pro ,但我不知道如何解决这个问题。

有一个名为onImageTap的属性,这就是答案。

这是我设置 Carousel 配置的代码的一部分。

                dotSpacing: 15.0,
                dotColor: Colors.blueGrey[100],
                dotIncreasedColor: Color.fromRGBO(203, 0, 0, 1),
                autoplay: true,
                autoplayDuration: Duration(seconds: 10),
                indicatorBgPadding: 0.0,
                dotBgColor: Colors.transparent,
                borderRadius: true,

标签: flutterflutter-layoutflutter-animation

解决方案


            dotSpacing: 15.0,
            dotColor: Colors.blueGrey[100],
            dotIncreasedColor: Color.fromRGBO(203, 0, 0, 1),
            autoplay: false,
            indicatorBgPadding: 0.0,
            dotBgColor: Colors.transparent,
            borderRadius: true,

编辑 -

提供解释——

当 imageTap 事件发生时,您必须设置自动播放属性。为此,请执行以下步骤

  1. 添加类属性bool isAutoPlayEnabled = true
  2. 在 CarouselOptions 中,设置autoplay: isAutoPlayEnabled
  3. onImageTap 做setState(){ isAutoPlayEnabled = ! isAutoPlayEnabled; }

现在将重新创建小部件树,并暂停轮播页面。

另一种理论-我不知道它是否会起作用-尝试用旋转木马包装GestureDetector并使用它onTap:来设置isAutoPlayEnabled


推荐阅读