Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter- GestureDetector not working with containers in stack

I have two containers in a stack and both containers have GestureDetector.The OnTap for the first container is working fine but it's not working with another container. The first container is the image and the second one is the green background aligned partially over the first container.

new Stack(             alignment: Alignment(0.0, 1.44),             children: <Widget>[               GestureDetector(                 onTap: () => _openImage(context),                 child: Container(                   width: 340.0,                   foregroundDecoration: new BoxDecoration(                       color: Color.fromRGBO(155, 85, 250, 0.55)),                   height: 240.0,                   child: FadeInImage.assetNetwork(                     placeholder: 'assets/dimlight.png',                     image: post.imageUrl,                     fit: BoxFit.cover,                   ),                 ),               ),               new GestureDetector(                 child: new Container(                   color: Colors.green,                   child: Row(                     mainAxisSize: MainAxisSize.max,                     children: <Widget>[                       SizedBox(width: 7.0),                       CircleAvatar(                         backgroundImage:                             new AssetImage("assets/boy.png")                         radius: 30.0,                       ),                       SizedBox(                         width: 7.0,                       ),                       Column(                         crossAxisAlignment: CrossAxisAlignment.start,                         children: <Widget>[                           new SizedBox(                             height: 20.0,                           ),                           Text(                             post.user.name,                             style: TextStyle(fontWeight: FontWeight.bold),                           ),                           Text(                             getTimeString(post.timestamp.toString()),                             style: TextStyle(                                 color: Colors.grey, fontSize: 10.0),                           ),                         ],                       ),                       SizedBox(                         width: 20.0,                       ),                     ],                   ),                 ),                 onTap: () => _navigateToDetails(context),               )             ],           ) 

Layout Screenshot

enter image description here

like image 563
Himanshu Yadav Avatar asked Oct 24 '18 09:10

Himanshu Yadav


People also ask

What is the difference between InkWell and GestureDetector flutter?

They both look the same and almost do the same thing, so what is difference between flutter InkWell vs GestureDetector? Generally speaking, GestureDetector provides more gesture control to detect almost every user interaction including dragging, swiping, pinching, etc.

What is Stack widget in flutter?

The stack is a widget in Flutter. It contains a list of widgets and places them on top of each other. And it places their children on top of each other like a stack of books. In other words, stack developers would overlap multiple widgets on one screen. As you can add different images or colors using containers in it.


2 Answers

Try setting the behavior property of GestureDetector to HitTestBehavior.translucent.

like image 147
Athul Sai Avatar answered Sep 18 '22 21:09

Athul Sai


Using InkWell instead of GestureDetector solved my problem.

like image 36
Sobhan Moradi Avatar answered Sep 19 '22 21:09

Sobhan Moradi