简体   繁体   中英

How to make dot indicator carousel slider in flutter using asset/images?

        CarouselSlider(
          options: CarouselOptions(
        
          ),
          items: [
   
            Image.asset('images/pizza.png'),
          ],
        ),

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

I don't know how to declare the dotIndicator so that the dot autoplay with the images.

You can use a variable to track the current index from 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(),
            )
          ],
        ),
      ),
    );
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM