Flutter onTap method for Containers
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(...),)
Screenshot:
You shouldn't use GestureDetector
because it won't show you any ripple effect (which is core part of a Material design app), so you can use InkWell
, here is the basic example.
Widget _buildContainer() { return Material( color: Colors.blue, child: InkWell( onTap: () => print("Container pressed"), // handle your onTap here child: Container(height: 200, width: 200), ), );}