Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: How to make an infinite loop in listview

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 ?

like image 610
luc Avatar asked Oct 25 '25 06:10

luc


2 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),
                  );
                })
like image 150
Marcelo Juan Cabrera Gutierrez Avatar answered Oct 27 '25 21:10

Marcelo Juan Cabrera Gutierrez


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}'),
        );
      },
    )
like image 39
Chirag Rathod Avatar answered Oct 27 '25 22:10

Chirag Rathod