Flutter Widget In Detail : AbsorbPointer & IgnorePointer
Detailed Explanation of AbsorbPointer & IgnorePointer Widgets

Search for a command to run...
Detailed Explanation of AbsorbPointer & IgnorePointer Widgets

No comments yet. Be the first to comment.
In this series, I am going to explain all the important Flutter widgets in detail.
Detailed Explanation of Container Widget
Lessons from Code, Content, and Consistency

Reliability basics: Outbox pattern + why it matters

Introducing Kafka/Redpanda + move to event-driven workflow

Connect Orders โ Inventory (first working service-to-service flow)

Inventory service + gRPC + proto contracts

null to the buttons onPressed property. But when there are too many buttons in your row/column or any other widget, it is very frustrating to pass this property one by one.Column. Now we want to disable all the buttons for some reason. For that, the common way is to go and write null to every button's onPressed property. Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(child:Text("1"),onPressed:null),
SizedBox(height:5.0),
ElevatedButton(child:Text("2"),onPressed:null),
SizedBox(height:5.0),
ElevatedButton(child:Text("3"),onPressed:null),
SizedBox(height:5.0),
ElevatedButton(child:Text("4"),onPressed:null),
SizedBox(height:5.0),
ElevatedButton(child:Text("5"),onPressed:null),
]
)


click events, but it also prevents scroll, drag, hover events.absorbing: When true, The widget prevents its subtree from receiving all kinds of events. When false, The widget will allow its subtree to receive the events.child: Pass widget/widgets from which you want to disable events.ignoringSemantics: This takes boolean as a parameter. true means widget should be ignored by screen readers when compiling the semantic tree.Stack(
alignment: AlignmentDirectional.center,
children: <Widget>[
SizedBox(
width: 200.0,
height: 100.0,
child: ElevatedButton(
onPressed: () {},
child: null,
),
),
SizedBox(
width: 100.0,
height: 200.0,
child: AbsorbPointer(
absorbing: true,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red.shade200,
),
onPressed: () {},
child: null,
),
),
),
],
);

onPressed property of the red button, it is not null, but still, it is not receiving any kind of event because we've wrapped our button inside the AbsorbPointer 
ignoring: If true it will ignore the pointer event.child: Pass widget/widgets from which you want to disable events.ignoringSemantics: If true the widget will be ignored when compiling the semantics tree. And will be ignored by the screen readersCenter(
child: IgnorePointer(
ignoring: ignoring,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Text('Ignoring: $ignoring'),
ElevatedButton(
onPressed: () {},
child: const Text('Click me!'),
),
],
),
),
),

ignoring from false to true , the button is no longer clickable.red box and the blue box. The red box is on top of the blue box. Stack(
alignment: Alignment.topCenter,
children: [
BlueBox(
onClicked: () {print("Blue box clicked")}.
child: Container(width: 200, height: 150),
color: Colors.blue,
),
RedBox(
onClicked: () {print("Red box clicked")}.
child: Container(width: 100, height: 100),
color: Colors.red,
),
]
)
onClicked property.
red box inside the IgnorePointer. Let's see what happens IgnorePointer(
child: RaisedButton(
onPressed: (){print("Red box clicked")},
child:Container(
child: Container(width: 100, height: 100),
color: Colors.red,
),
)
)
Output :

As we can observe that now the red box is not clickable anymore. Even if we click on the red box the blue box will be clicked.
Now let's wrap the same red box with the AbsorbPointer and see what happens.
AbsorbPointer(
child: RaisedButton(
onPressed: (){print("Red box clicked")},
child:Container(
child: Container(width: 100, height: 100),
color: Colors.red,
),
)
)

Now you can see that the red box is not even showing that click event/hand cursor.
blue box below the red box.IgnorePointer is also ignoring the interaction of the child widget. But it will not ignore the widget below it.
Simply put, In AbsorbPointer what is happening is that The
redbox contains some part of thebluebox below it, that's why it is also absorbing/ignoring that part and disable it.
But IgnorePointer will not absorb/ignore that below part and it will make the whole box clickable by not considering the
redbox click event
GestureDetector widget.GestureDetector(
onTap: (){
print("Gesture Detector");
},
child:ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.transparent),
onPressed: (){
print("Red Button Pressed");
},
child:Container(width: 100, height: 100,color:Colors.red),
)
)

Red Button Pressed as expected. Notice that it will not call the onTap function of the GestureDetector widget.ElevatedButton inside our IgnorePointer.GestureDetector(
onTap: (){
print("Gesture Detector");
},
child: IgnorePointer(
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.transparent),
onPressed: (){
print("Red Button Pressed");
},
child:Container(width: 100, height: 100,color:Colors.red),
)
)
)
GestureDetector or ElevatedButton ??IgnorePointer will ignore its child as well as its whole widget tree.. It means it will also ignore the GestureDetector because it is also a part of the IgnorePointers widget tree.
Now let's wrap our button inside AbsrobPointer.
GestureDetector(
onTap: (){
print("Gesture Detector");
},
child: AbsorbPointer(
child: ElevatedButton(
style: ElevatedButton.styleFrom(primary: Colors.transparent),
onPressed: (){
print("Red Button Pressed");
},
child:Container(width: 100, height: 100,color:Colors.red),
)
)
)

ElevatedButton is ignored but the onTap of the GestureDetector is working. Because the AbsorbPointer will only absorb the child of it. Not the whole widget tree.That's IT. That's all you need to about AbsorbPointer and IgnorePointer. Hope you understood. ๐
Previous Blog : Widget In Detail - Container