获取在Flutter中点击网格项时的网格视图计数列信息的方法。

huangapple go评论67阅读模式
英文:

How to get Grid view count column information upon clicking grid item in flutter

问题

Hi 开发者们,我一直在我的Flutter应用中使用GridView来显示一些数据。我的GridView设置了crossAxisCount: 5,即5列。当用户从GridView中点击一个项目时,我想要更多的信息,比如用户点击了哪一列,类似下面的样子:

获取在Flutter中点击网格项时的网格视图计数列信息的方法。

所以,当用户从GridView中点击info3info8时,是否有可能或者有方法可以知道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

获取在Flutter中点击网格项时的网格视图计数列信息的方法。

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"),
          );
        });
  }

huangapple
  • 本文由 发表于 2023年5月8日 00:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定