How do i make the 2nd box consume clicks so that i...
# compose
a
How do i make the 2nd box consume clicks so that it doesn't let its parent consume them? I have used
pointerInput(Unit){}
as some folks suggested and it's what
Surface
uses internally, but it still lets the clicks go through its parents. Chatgpt suggested to use
detectTapGestures {  }
inside of the pointerInput() and it does work (on desktop at least, havent tried others) but do I really need it? I want it to just block any kind of gestures. am i missing something?
Copy code
Box(Modifier.clickable { error("Click parent") }.background(Color.Gray).fillMaxSize()) {

   Box(Modifier.size(300.dp).background(Color.White).pointerInput(Unit) {}) { // this should consume clicks done to it, and not let 'parent' get clicks

        Box(Modifier.size(48.dp).background(Color.Blue).clickable { println("👍 Clicking this is fine") })
   }
}
c
Hm,
Surface
is doing
.pointerInput(Unit) {}
to prevent click to itself. not sure how your
pointerInput
on a child should prevent the parent’s
clickable
be called. It’s “empty” on
Surface
so it doesn’t do anything.
as some folks suggested
where is that?
a
in the code above the 2nd box is the container/surface alternative, so i am trying to prevent clicks on itself.
c
So on which box do you expect what?
I guess now you always trigger the exception, right?
a
right now (bad behavior), tapping the 2nd box causes an exception to happen yes. the 3rd box is clickable and prints the log which is good what i want is tapping on the 2nd box to not let the click go to the 1st box (so no exception is thrown). i still want the 3rd box to print the logs when clicked as it is rn
c
Yeah, that’s not how it works. In the discussion they are talking about click-through. So if you click the second box. The clickable if the first box is always triggered.
a
im confused. isn't the 2nd box allowing the clicks go to the parent called a click-through?
c
No click-through is to the child. So like in layers or sheets of paper. If you press the top layer down, all layers below will also somhow “feel” that the top layer is pressed down.
a
gotcha. if that's the case yeah, i want the other way around
d
Changing
Copy code
.pointerInput(Unit) {}
to
Copy code
.pointerInput(Unit) {
    detectTapGestures(onTap = { })
}
blocks the click from going to the parent. Is that what you are looking for?
a
@Dan MacNeil yes that does work. thanks