首页 > 解决方案 > Flutter - 自动滚动到 CaroidrlSlider

问题描述

我正在尝试启用自动滚动到卡片。它应该垂直滚动。我希望 Autoscroll 无需任何用户操作即可工作。并在 5 秒内自动滚动。

这是我要滚动的代码的一部分:

 Container(
          width: 3000,
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: CarouselSlider(
              options: CarouselOptions(height: 150.0),
              items: [1, 2, 3, 4, 5].map((i) {
                return Builder(
                  builder: (BuildContext context) {
                    return Container(
                      width: MediaQuery.of(context).size.width,
                      margin: EdgeInsets.symmetric(horizontal: 5.0),
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(30),
                          color: greenHex),
                      child: Row(
                        children: [
                          Padding(
                            padding: const EdgeInsets.only(
                                top: 0, right: 10, left: 0, bottom: 8),
                            
                          ),
                        ],
                      ),
                    );
                  },
                );
              }).toList(),
            ),
          ),
        ),

谁能帮帮我?

标签: flutterdartscrollvertical-scrollingautoscroll

解决方案


您需要在 Carousel Slider Options 中启用 autoPlay 并设置 autoPlayInterval。

例子:

        CarouselSlider.builder(
          options: CarouselOptions(
            .
            .
            .
            autoPlay: true,
            autoPlayInterval: const Duration(seconds: 4),
            enableInfiniteScroll: true,
          ),
        ),

推荐阅读