英文:
How to get Grid view count column information upon clicking grid item in flutter
问题
Hi 开发者们,我一直在我的Flutter应用中使用GridView来显示一些数据。我的GridView设置了crossAxisCount: 5
,即5列。当用户从GridView中点击一个项目时,我想要更多的信息,比如用户点击了哪一列,类似下面的样子:
所以,当用户从GridView中点击info3和info8时,是否有可能或者有方法可以知道col3被点击了呢?谢谢。
英文:
Hi developers i have been using gridview in my flutter app to display some data's my gridview which has crossAxisCount: 5
which is 5 columns so when clicking an item from gridview i want more information like which column user have clicked like below
so when user clicks info3 and info8 from the gridview is it possible or a way we can get col3 is clicked? thanks
答案1
得分: 1
以下是翻译好的内容:
是的,简单的逻辑可以完成你的工作。只需将索引与crossAxisCount取模。类似这样。
@override
Widget build(BuildContext context) {
int crossAxisCount = 4;
return GridView.builder(
itemCount: 40,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount),
itemBuilder: (_, int index) {
return InkWell(
onTap: () {
print("第${(index % crossAxisCount) + 1}列");
},
child: Text("项目 $index"),
);
});
}
英文:
Yes, Simple logic can do your job. Just do modulo of index with crossAxisCount. Something like this.
@override
Widget build(BuildContext context) {
int crossAxisCount = 4;
return GridView.builder(
itemCount: 40,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount),
itemBuilder: (_, int index) {
return InkWell(
onTap: () {
print("Col${(index % crossAxisCount) + 1}");
},
child: Text("Item $index"),
);
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论