flutter 如何使用素材/图片制作抖动中的点指示器转盘滑块?

v7pvogib  于 2022-12-19  发布在  Flutter
关注(0)|答案(1)|浏览(144)
CarouselSlider(
          options: CarouselOptions(
        
          ),
          items: [
   
            Image.asset('images/pizza.png'),
          ],
        ),

        DotsIndicator(
            dotsCount: 4,
            position: dotPosition.toDouble(),
//declare the position to autoplay
             
            ),
        ),

我不知道如何声明dotIndicator,以便点自动播放图像。

blmhpbnm

blmhpbnm1#

可以使用变量从onPageChanged跟踪当前索引。

class _MyWidgetState extends State<MyWidget> {
  final items = [
    Image.asset('images/drumstick.png'),
    Image.asset('images/pizza.png')
  ];

  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            CarouselSlider(
              options: CarouselOptions(
                autoPlay: true,
                aspectRatio: 2.0,
                enlargeCenterPage: true,
                onPageChanged: (index, reason) {
                  setState(() {
                    currentIndex = index;
                  });
                },
              ),
              items: items,
            ),
            DotsIndicator(
              dotsCount: items.length,
              position: currentIndex.toDouble(),
            )
          ],
        ),
      ),
    );
  }
}

相关问题