颤振:如何知道列表视图中的项目位置?

问题描述 投票:-1回答:3

我有项目的列表视图,但我很想知道如何在列表视图中找到它的当前位置,有人知道吗?

flutter flutter-layout flutter-dependencies flutter-animation flutter-test
3个回答
1
投票

使用像这样的ListView.builder

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

索引表示项目的位置。


0
投票

你可以使用ListView.builder

ListView.builder
  (
    itemCount: litems.length,
    itemBuilder: (BuildContext ctxt, int index) {
      //the index is the item position you are looking for
     return new Text('my Index is: $index');
    }
  )

0
投票

我相信你的问题不属于这种情况:

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

而是关于如何知道你当前在哪里滚动:

我建议遵循这个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"));
                },
              ),
            ),
          ),
© www.soinside.com 2019 - 2024. All rights reserved.