Flutter - Detect Tap on the Screen that is filled with other Widgets Flutter - Detect Tap on the Screen that is filled with other Widgets dart dart

Flutter - Detect Tap on the Screen that is filled with other Widgets


Use GestureDetector's behavior property and pass HitTestBehavior.opaque to it, which recognizes entire screen and detects the tap when you tap anywhere on the screen.

body: GestureDetector(          behavior: HitTestBehavior.opaque,          onTap: () => print('Tapped'),          child: QQBody(),        ),

Hope this answers your question.


Please use below code--

class QQHome extends StatelessWidget {  @override  Widget build(BuildContext context) {    return MaterialApp(      debugShowCheckedModeBanner: false,      theme: ThemeData(        primaryColor: Colors.blueGrey,      ),      home: Scaffold(        appBar: AppBar(          centerTitle: true,          title: Text('QuoteQuota'),        ),        body: GestureDetector(          onTap: () => print('Tapped'),          child: Container(          height : MediaQuery.of(context).size.height,          width : MediaQuery.of(context).size.width,),        ),      ),    );  }}


Like Darshan said, you can trigger Tap by wrapping the body inside a Gesture detector like this...

body: GestureDetector(          behavior: HitTestBehavior.opaque,          onTap: () => print('Tapped'),          child: QQBody(),        ),

Also in some cases, you may need to avoid trigger clicking on other widgets while tapping anywhere in body. That can be solved by using IgnorePointer Widget

body: GestureDetector(      behavior: HitTestBehavior.opaque,      onTap: () {        print('This will click');      },      // -------- No Widget below tree will be trigger click.      child: IgnorePointer(        ignoring: ClassLibrary.selected != null ? true : false,        child: TabBarView(          children: [                   ...