Flutter gesturedetector pass through

Flutter Gesture Detector – Pass Through

The Flutter GestureDetector widget provides various callbacks to handle gestures, such as taps, drags, and scaling. By default, it consumes the gestures and does not allow them to propagate to its parent or other widgets below it. However, if you want to pass the gestures through the GestureDetector to the underlying widgets, you can use the ‘behavior’ property with the value ‘HitTestBehavior.passThrough’.

Example:

  
import 'package:flutter/material.dart';

class PassThroughExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Gesture Detector - Pass Through Example')),
      body: Center(
        child: GestureDetector(
          behavior: HitTestBehavior.passThrough,
          onTap: () {
            print('Gesture detected');
          },
          child: Container(
            padding: EdgeInsets.all(16.0),
            color: Colors.blue,
            child: Text(
              'Tap Me',
              style: TextStyle(
                color: Colors.white,
                fontSize: 20.0,
              ),
            ),
          ),
        ),
      ),
    );
  }
}

void main() {
  runApp(MaterialApp(
    home: PassThroughExample(),
  ));
}
  
  

In this example, we have a GestureDetector wrapped around a Container with a text widget inside. By setting the behavior property to HitTestBehavior.passThrough, the GestureDetector allows the tap gesture to pass through to the Container, triggering the onTap callback of the GestureDetector as well. This will print ‘Gesture detected’ in the console when the container is tapped.

Leave a comment