Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with Tap function within LIstView

I was testing the "tap function" for items within a ListView, but it does not seem to work. The print function does not work when I tap upon the list.

return Scaffold(
          appBar: AppBar(
            // App Bar
            title: Text(
              "ListView On-Click Event",
              style: TextStyle(color: Colors.grey),
            ),
            elevation: 0,
            backgroundColor: Colors.white,
          ),
          // Main List View With Builder
          body: ListView.builder(
              itemCount: imgList.length,
              itemBuilder: (context, index) {
                return GestureDetector(
                  onTap: () {
                    print("button pressed");
                    print(index);
                  },
                  child: Container(
                    margin: const EdgeInsets.symmetric(
                      vertical: 2.0,
                      horizontal: 8.0,
                    ),
                    child: Stack(
                      children: <Widget>[
                        cardDesign,
                        cardImage,
                      ],
                    ),
                  ),
                ); // gesturedetector
              }));

Where am I going wrong?

like image 227
simsim Avatar asked Sep 10 '25 22:09

simsim


2 Answers

Try to used Column instead of Stack

ListView.builder(
          shrinkWrap: true,
          itemCount: 10,
          itemBuilder: (context, index) {
            return GestureDetector(
               onTap: () {
                print("button pressed");
                print(index);
              },
              child: Container(
                margin: const EdgeInsets.symmetric(
                  vertical: 2.0,
                  horizontal: 8.0,
                ),
                child: Column(
                  children: <Widget>[
                    Text(index.toString()),//put your widgets here
                  ],
                ),
              ),
            ); // gesturedetector
          }),
like image 52
Ravindra S. Patil Avatar answered Sep 13 '25 14:09

Ravindra S. Patil


//you can do like this

ListView.builder(
              itemCount: 5,
              scrollDirection: Axis.horizontal,
              itemBuilder: (context, int index) {
                return VCContainer(
                  onTap: () {
                    for (int i = 0; i < 5; i++) {
                      if (i == index) {
                        setState(() {
                          selectedList[i] = true;
                        });
                      } else {
                        setState(() {
                          selectedList[i] = false;
                        });
                      }
                    }
                  },
                );
              },
            ),


// in VCContainer Class

VCContainer({required this.onTap});


  VoidCallback onTap;

Widget build(BuildContext context) {
    return 
        InkWell(
          onTap: onTap,
          child: Image.asset(
            imageList[index],
            width: 60,
            height: 60,
          ),
    );
  }
like image 42
Manish Dhakrey Avatar answered Sep 13 '25 14:09

Manish Dhakrey