Ad

Flutter - Show How Many Items Scrolled In Gridview

- 1 answer

i am trying to implement like the below image.

enter image description here

I have a grid view which has 2 columns in which i am displaying my list.

enter image description here

As you can see in the above image it is showing 175/67 products.

my logic is i am giving my grid view a scroll controller.

I am adding a listener to that controller but i think my logic or calculation is wrong.

below is my code :

ScrollController _scrollController = new ScrollController();

in initstate i am giving adding a listener to scroll controller

_scrollController.addListener(_scrollListener);

void _scrollListener() {
    setState(() {
      debugPrint(_scrollController.offset.toString());
      debugPrint(count.toString());
      debugPrint(((_scrollController.offset / count).round()).toString());
      index = (_scrollController.offset / count).round();
      print(index);
    });
  }

the count is the total items in my list i.e 67.

as i scroll down it is giving wrong output.

please tell me where i am going wrong.

where my logic has gone wrong?

below is my grid view code:

DragSelectGridView(
                      shrinkWrap: true,
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        childAspectRatio: (itemWidth / itemHeight),
                        crossAxisCount: 2,
                      ),
                      physics: ClampingScrollPhysics(),
                      gridController: gridController,
                      scrollController: _scrollController,
                      itemCount: items.length,
                      itemBuilder: (context, index, selected) {
                       
                        return ProductCard(
                          data: items[index],
                          isSelected: selected,
                        );
                      },
                    ),

Thanks in Advance!!!

Ad

Answer

look at this code and see dose it useful or not

 NotificationListener<ScrollNotification>(
                                onNotification: (notification) {
                                  //get height of each item
                                  var height=notification.metrics.maxScrollExtent/items.length;
                                  //get position of item
                                  var position=((notification.metrics.maxScrollExtent-notification.metrics.extentAfter)/height).round();
                                  print('postion is $position and the lenght is ${_catcontroller.products.length}');
                                  return true;
                                },
Ad
source: stackoverflow.com
Ad