【问题标题】:Getting Index From Carousel slider -Flutter从轮播滑块获取索引 -Flutter
【发布时间】:2021-12-08 23:00:34
【问题描述】:

我在我的代码中使用轮播滑块包,我正在尝试从此滑块中提取索引以添加到滑块底部的点指示器: 我正在使用 2 个包:https://pub.dev/packages/carousel_sliderDot Indicator

 Padding(
              padding: EdgeInsets.fromLTRB(
                  size.width * 0.05, 35, size.width * 0.05, 0),
              child: CarouselSlider(
                options: CarouselOptions(
                  aspectRatio: 2.0,
                  enlargeCenterPage: true,
                  scrollDirection: Axis.horizontal,
                  autoPlay: true,
                ),
                items: [
                  Card(
                    clipBehavior: Clip.antiAlias,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Image(
                      image: AssetImage("assets/greenhouse.jpg"),
                    ),
                  ),
                  Card(
                    clipBehavior: Clip.antiAlias,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(20),
                    ),
                    child: Image(
                      image: AssetImage("assets/greenhouse.jpg"),
                    ),
                  ),
                ],
              ),
            ),
            new DotsIndicator(
              dotsCount: 5,
              position: 1,
              decorator: DotsDecorator(
                color: Colors.grey,
                activeColor: greencol,
              ),
            ),

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies


    【解决方案1】:

    使用 onPageChange 保存您的索引:

     int _currentIndex = 0;
    
     Padding(
                  padding: EdgeInsets.fromLTRB(
                      size.width * 0.05, 35, size.width * 0.05, 0),
                  child: CarouselSlider(
                    options: CarouselOptions(
                      aspectRatio: 2.0,
                      enlargeCenterPage: true,
                      scrollDirection: Axis.horizontal,
                      autoPlay: true,
                      onPageChanged: (index, reason) {
                        _currentIndex = index;
                        setState((){});
                      },
                    ),
                    items: [
                      Card(
                        clipBehavior: Clip.antiAlias,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Image(
                          image: AssetImage("assets/greenhouse.jpg"),
                        ),
                      ),
                      Card(
                        clipBehavior: Clip.antiAlias,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(20),
                        ),
                        child: Image(
                          image: AssetImage("assets/greenhouse.jpg"),
                        ),
                      ),
                    ],
                  ),
                ),
                new DotsIndicator(
                  dotsCount: 5,
                  position: _currentIndex.toDouble(),
                  decorator: DotsDecorator(
                    color: Colors.grey,
                    activeColor: greencol,
                  ),
                ),
    

    【讨论】:

    • 它只工作了一个小问题,该位置不采用整数变量,而是您必须将其转换为双精度数,感谢您的回答,它解决了我的问题欢呼。
    • 更新了答案,很高兴有帮助~
    猜你喜欢
    • 2020-05-25
    • 2018-03-29
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多