is it possible to not clip a child composable if i...
# compose
j
is it possible to not clip a child composable if its constraints are bigger than the parent composable ? for example in the following example, i dont want to clip
the red color box
? similar to something like
clipChildren=false
in view system?
Copy code
@Composable
fun MyBox() {
    Box(
        Modifier
            .fillMaxWidth()
            .height(1.dp) // Restrict the parent's height to 1.dp
    ) {
        Box(
            Modifier
                .fillMaxWidth()
                .height(8.dp) // Child is larger than the parent
                .background(Color.Red)
        )
    }
}
s
There is no default clipping in compose. What you're experiencing probably is that the child wants to be 8, but the parent is giving constraints that are smaller than that, so it doesn't let it be bigger. You'd have to either • Use requiredHeight so that it does not respect the given constraints • Use wrapContentHeight(alignment= ..., unbounded = true) and then set the height, so that you can also control the direction in which it will grow while going out of the give constraints.
j
Copy code
@Composable
    fun MyBox() {
        Box(
            Modifier
                .fillMaxWidth()
                .height(1.dp) // Restrict the parent's height to 1.dp
        ) {
            Box(
                Modifier
                    .fillMaxWidth()
                    .requiredHeight(50.dp) // Child is larger than the parent
                    .background(Color.Red)
            )
        }
    }
tried this, but this also doesn't seem to work
n
what is your issue little bit i am not understand
don't use clicp
s
What is the result of this? Got a screenshot?
j
If you do manual drawing via
Modifier.drawBehind {}
I believe you can draw beyond the bounds of the attached composable. I’ve done this to draw custom drop shadows. With that being said, you can only perform primitive drawing with this method. You cannot layout child Composables beyond the parents bounds. If you’re trying to draw a custom background this might be the method you want to explore.
s
You can draw beyond bounds in any composable, compose does not clip anything by default unless you're clipping somewhere yourself. You just need to not follow the parent's constraints and that's it, you can draw outside.
Yup, just doing:
Copy code
@Preview
@Composable
private fun Preview() {
  Surface {
    Box(Modifier.size(100.dp), contentAlignment = Alignment.Center) {
      Box(
        Modifier
          .fillMaxWidth()
          .height(10.dp), // Restrict the parent's height to 10.dp
      ) {
        Box(
          Modifier
            .fillMaxWidth()
            .requiredHeight(50.dp) // Child is larger than the parent
            .background(Color.Red),
        )
      }
    }
  }
}
Already gives the right result as shown in the screenshot. So if you do not see the same thing, the mistake is not in this part of the code, but somewhere else.
If you add a little
.border(1.dp, Color.Yellow)
in the "10.dp" box, just for illustration purposes, you get this
j
Got it. Found my mistake, thank you 🙏