22

Is there any way to prevent my screen from receiving touch events, I don't want to disable touch for every Widget in my app. I just want to lock the app so that it doesn't receive touch events. How can I do that?

3 Answers3

65

You can wrap your widget in AbsorbPointer and it won't receive touches. To enable the touch again, you can set absorbing: false

AbsorbPointer(
  child: YourWidget(...),
);
7

Two way to do :

  1. AbsorbPointer
  2. IgnorePointer

Check difference with example here :

Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78
1

Lets see a practical example of using IgnorePointer widget.

This case is pretty common when we started trying to implement something like toggling a selection on a widget to delete or something like this.

RESULT:

Example Senario : Holding on a WhatsApp message and delete option coming on top. if tap anywhere else while this option active, it will go.

I implemented it like this.

appBar: AppBar(
         title: Text('MyApp'), 
         actions: [
            if (_selected != null) // <-- Delete button only appear something selected.
               IconButton(
               icon: Icon(Icons.delete),
               onPressed: () { 
                   // Delete Code here
               }
         ]
      ),
body: GestureDetector(
           behavior: HitTestBehavior.opaque,
           onTap: () { 
                 print('Tapped');
                 setState(() { _selected = null });
           },
           child: IgnorePointer(
           ignoring: _selected != null ? true : false, // <-- Ignore only when selecting something.
           child: Column(
             children: [
              ...

              // This is a sample message 

              GestureDetector(
              onLongPress: () {  
                 setState(() { _selected = messageId  });
              }
              child: Container(
                 child: Text('This is a message'),
              ),
               
              ...

Hope it will help somebody! Have a nice day.

MBK
  • 2,589
  • 21
  • 25