从轮播滑块获取索引 -Flutter

flu*_*ser 5 flutter flutter-dependencies flutter-layout

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

 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,
              ),
            ),
Run Code Online (Sandbox Code Playgroud)

Jim*_*hiu 8

使用 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,
              ),
            ),
Run Code Online (Sandbox Code Playgroud)