Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter onTap method for Containers

Been developing a flutter app and dynamicly building some containers from some Firebase data. I wanted to know if there is a way to get a onTap method for containers (or any widget which is not a button ?

Here is a code sample :

  child: new Container(      //INSERT ONTAP OR ONPRESSED METHOD HERE      margin: const EdgeInsets.symmetric(vertical: 10.0),     child: new Row(       crossAxisAlignment: CrossAxisAlignment.start,       children: <Widget>[         new Container(           margin: const EdgeInsets.only(right: 16.0),           child: new GoogleUserCircleAvatar(snapshot.value['images'][0]),         ),         new Column(           crossAxisAlignment: CrossAxisAlignment.start,           children : [             new Container(               padding: const EdgeInsets.only(bottom: 8.0),               child: new Text("${snapshot.value['name']}",                 style: new TextStyle(                   fontWeight: FontWeight.bold,                 ),               ),              ),             new Text("${snapshot.value['description']}",               style: new TextStyle(                 color: Colors.grey[500],               ),             ),           ]         )       ],     ), 
like image 845
Alexi Coard Avatar asked Jun 01 '17 21:06

Alexi Coard


People also ask

What is use of onTap in flutter?

Using the Gesture Detector GestureDetector( onTap: () { print("Container was tapped"); }, child: Container(...), ) The only difference between the two is InkWell provides a ripple effect when the pointer is in contact with the screen while this is no such visual feedback with GestureDetector.

How do you put containers in a container flutter?

To size a Container that resides inside another Container, you need to set the alignment property of the parent Container. Otherwise, the child Container won't care about the width and height you set for it and will expand to its parent's constraints.

What is difference between onTap and onPressed?

onPressed is usually on buttons while onTap is For any other widget you add behavior to with smtin lyk InkWell or GestureDetector.


2 Answers

You can wrap your Container in an InkWell or GestureDetector. The difference is that InkWell is a material widget that shows a visual indication that the touch was received, whereas GestureDetector is a more general purpose widget that shows no visual indicator.

like image 109
Collin Jackson Avatar answered Oct 03 '22 13:10

Collin Jackson


wrapping the container inside an Inkwell() Widget could solve the problem or even GestureDetector() as

  InkWell(                                 child: Container(...),                                 onTap: () {                                   print("tapped on container");         },                            ); 

Using the Gesture Detector

GestureDetector(   onTap: () { print("Container was tapped"); },   child: Container(...), ) 
like image 23
Mahesh Jamdade Avatar answered Oct 03 '22 15:10

Mahesh Jamdade