fluttergridviewflutter-gridview

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


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

enter image description here

so when user clicks info3 and info8 from the gridview is it possible or a way we can get col3 is clicked? thanks


Solution

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