'Flutter count currently visible items of a List View

I want to show the counter on the List View that will show currently visible items of the list view / total items in the list view.

Suppose that I have 15 items in the list that I have allocated to the list view, and the items on the screen that are visible are 4/15. 4 items are those which the visible on the screen.

And when I scroll as next items get visible, the counter should be incremented and show the currentlyVisibleItems/totalItems.

I am trying like:

ScrollController scrollController;
final itemSize = 15;

scrollController = ScrollController();
scrollController.addListener(_scrollListener);

int scrolledIndex=0;
  void _scrollListener() {
    scrolledIndex = (scrollController.offset / itemSize).round() + 1;
    debugPrint("ad78798h2:::"+scrolledIndex.toString()+"/"+"15");
  }

and my list view is:

          ListView.builder(
            controller: controller.scrollController,
            shrinkWrap: true,
            scrollDirection: Axis.horizontal,
            itemCount: controller.calculateInstallmentsChipsHelperModel.length,
            itemBuilder: (BuildContext context, int index) {
              return getListViewChildItems(context, index);
            },
          ),

I simple want to show the counter for the currently visible items above the listview!

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source