Flutter scroll controller infinite scroll fail on larger screen size

Viewed 18

I have a problem with infinite scroll in flutter, I am loading six items per page and loading more as I scroll , this works perfectly with a smaller screen size but the problem comes in when I use an emulator with a larger screen size, when the screen size is larger then the first six items don't fill the screen hence I cannot scroll in order to load the other items.

Does anybody have an idea of how I can get around this?

Thanks

1 Answers

You can use ListView.builder. The ListView.builder constructor takes an IndexedWidgetBuilder, which builds the children on demand. This constructor is appropriate for list views with a large (or infinite) number of children because the builder is called only for those children that are actually visible.

return ListView.builder(
  padding: const EdgeInsets.all(10.0),
  itemBuilder: (context, i) {
    if (i.isOdd) return const Divider();

    final index = i ~/ 2; 
    if (index >= _listItems.length) {
      _listItems.addAll(_newItems); //load more items here
    }
    return yourWidget(_listItems[index]);
  },
);

This way no need to use scrollController. You can refer to this codelab for more info.

Related