Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Dart Flutter GridView items to click?

I'm trying to make an interface with Dart Flutter. I made an interface like this:

enter image description here

I want these items to be clickable. When clicked, I will take action. What can I do so that items can be clicked?

Codes:

body: Center(
        child: GridView.builder(
          gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
          ),
          itemCount: subjects.length,
          itemBuilder: (BuildContext context, int index) {
            return Container(
              child: Card(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset(subjects[index].subjectImage, fit: BoxFit.cover, height: 50, width: 50,),
                      SizedBox(height: 10,),
                      Text(subjects[index].subjectName, style: TextStyle(fontSize: 20),),
                    ],
                  ),
                ),
            );
          },
        ),
      )
like image 423
Emir Bolat Avatar asked Sep 18 '25 11:09

Emir Bolat


2 Answers

You can use GestureDetector or Inkwell for this.And write your onpress action code in onTap().

     GestureDetector(
    onTap: () {
      // To do
    },
    child: Card(
     
    ),
  ),

or

InkWell(
    onTap: () {
      // To do
    },
    child: Card(),
  ),
like image 134
Sobin Benny Avatar answered Sep 20 '25 15:09

Sobin Benny


You can use GestureDetector or Inkwell for Press your any Widget in flutter using onTap() function

GestureDetector(
    onTap: () {
      // Write your code here
    },
    child: Container(),
  ),

or use inkwell like

 InkWell(
        onTap: () {
          // Write your code here
        },
        child: Container(),
      ),
like image 29
Ravi Limbani Avatar answered Sep 20 '25 14:09

Ravi Limbani