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], ), ), ] ) ], ),
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.
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.
onPressed is usually on buttons while onTap is For any other widget you add behavior to with smtin lyk InkWell or GestureDetector.
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.
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(...), )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With