2

I have a list view of items but I would love to know how I can find its current position inside a listview, does anyone know?

Dead Pool
  • 167
  • 4
  • 15

2 Answers2

2

Use a ListView.builder like this:

ListView.builder(
    itemCount: items.length,
    itemBuilder: (BuildContext context, int index) {
     return new Text('The Position is: $index');
    }
)

The index represents the position of the item.

JideGuru
  • 7,102
  • 6
  • 26
  • 48
1

I believe your question does not fall into this situation:

ListView.builder
  (
    itemCount: litems.length,
    itemBuilder: (BuildContext ctxt, int index) {      
     return new Text('my Index is: $index');
    }
  )

But rather on how to know where are you currently scrolled:

I would suggest to follow this article

Expanded(
            child: NotificationListener<ScrollNotification>(
              onNotification: (scrollNotification) {
                // Basically you need something like:
                // final _index = scrollOffset (or position) / item height; 

              },
              child: ListView.builder(
                itemCount: 30,
                itemBuilder: (context, index) {
                  return ListTile(title: Text("Index : $index"));
                },
              ),
            ),
          ),
Durdu
  • 4,649
  • 2
  • 27
  • 47