Dart/Flutter Is there a way to find the index of my carousel?

Is there a way to find the index of a carousel item so I can use it to retrieve the related text and link associated with the items shown??

So I have 3 lists, a list for images, a list for text, and a list for urls.

Is there a way to find the associated index/key to reuse??

         child: Container(
                      child: CarouselSlider(
                        options: CarouselOptions(
                          enlargeCenterPage: true,
                          enableInfiniteScroll: true,
                          autoPlay: true,
                          // pageSnapping: true,
                        ),
                        items: listThumbs.map((e) => ClipRRect(
                          borderRadius: BorderRadius.circular(8),
                          child: Stack(
                            //fit: StackFit.expand,
                            children: <Widget>[
                            Image.network(e,
                            width: 1050,
                            height: 350,
                            fit: BoxFit.cover,
                            ),
                          Text(videObj.results[INDEX].name),
                         ] ),
                        )).toList(),
                      )),

2 answers

  • answered 2021-07-14 16:04 Lucas Josino

    You can try using the builder method.

    CarouselSlider example

    
    CarouselSlider.builder(
      itemCount: 15,
      itemBuilder: (BuildContext context, int itemIndex, int pageViewIndex) =>
        Container(
          child: Text(itemIndex.toString()),
        ),
    )
    

    This method will give the index of every item.

  • answered 2021-07-14 16:06 KuKu

    You can call 'indexOf' in the map iteration area.

    int idx = listThumbs.indexOf(e);
    
             child: Container(
                          child: CarouselSlider(
                            options: CarouselOptions(
                              enlargeCenterPage: true,
                              enableInfiniteScroll: true,
                              autoPlay: true,
                              // pageSnapping: true,
                            ),
                            items: listThumbs.map((e) {
                              // Here you can find item index.
                              int idx = listThumbs.indexOf(e);
    
                              return ClipRRect(
                                borderRadius: BorderRadius.circular(8),
                                child: Stack(
                                  //fit: StackFit.expand,
                                  children: <Widget>[
                                    Image.network(e,
                                      width: 1050,
                                      height: 350,
                                      fit: BoxFit.cover,
                                    ),
                                    Text(videObj.results[INDEX].name),
                                  ]),
                                 );
                                }
                              )).toList(),
                          )),
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum