Flutter: How to make an infinite loop in listview

Viewed 1220

I am trying to make an infinite loop in my listview, when index is at the end and user continue to scroll to left the next item will be the first one

ListView.builder(
                controller: _controller,
                scrollDirection: Axis.horizontal,
                itemCount: packs.length,
                itemBuilder: (context, index) {
                  _index = index;
                  print(index);
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                    ],
                  );
                },
              ),

I want user can infinite swipe, how can I do ?

3 Answers
  ListView.builder(
                physics: BouncingScrollPhysics(),
                scrollDirection: Axis.horizontal,
                // itemCount: items.length,
                itemBuilder: (context, index) {
                  final i = index % items.length;//<----to the right
                  final item = items[i];
                  return ItemSwiper(
                    item: item,
                    onTab: () => _.goToDetailItem(item: item),
                  );
                })

you can remove itemCount and try... But you cannot use a particular index, just showing a widget on that position. And you can print Index but make sure that you don't use an index with any external data source having limited data.

ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: Text('Item ${index + 1}'),
        );
      },
    )

you can use the package carousel_slider to achieve this for both directions

Related