我有项目的列表视图,但我很想知道如何在列表视图中找到它的当前位置,有人知道吗?
使用像这样的ListView.builder
:
ListView.builder(
itemCount: items.length,
itemBuilder: (BuildContext context, int index) {
return new Text('The Position is: $index');
}
)
索引表示项目的位置。
你可以使用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');
}
)
我相信你的问题不属于这种情况:
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"));
},
),
),
),