How we can show this RED view offset 30% from bel...
# compose
k
How we can show this RED view offset 30% from below ?
Copy code
@Composable
fun TicketCard() {
    Box(
        modifier = Modifier.fillMaxSize(),
    ) {
        ....background...
        Box(
            modifier = Modifier
                .padding(bottom = 300.dp)
                .size(100.dp)
                .background(Color.Red)
                .align(Alignment.BottomCenter)
        ) {

        }
    }
}
This is current implementation
n
I think you can make your own Modifier.offset() modifier
k
@nitrog42 how can i create ?
Any reference ?
n
yes here's my code :
Copy code
@Composable
fun TicketCard() {
    Box(
        modifier = Modifier.fillMaxSize(),
    ) {
        Box(
            modifier = Modifier
                .offsetPercent(0.70f)
                .size(100.dp)
                .background(Color.Red)
        ) {
        }
    }
}

private fun Modifier.offsetPercent(vertical: Float) = this.then(OffsetModifier(vertical))

private class OffsetModifier(
    val vertical: Float
) : LayoutModifier {
    override fun MeasureScope.measure(
        measurable: Measurable,
        constraints: Constraints
    ): MeasureResult {
        val placeable = measurable.measure(constraints)
        return layout(placeable.width, placeable.height) {
            placeable.placeRelative(0, (constraints.maxHeight * vertical).roundToInt())
        }
    }
}
it's a very rough way to do it, I'm not sure it would be compose-approved 😄 Using ConstraintLayout might be "safer" to do but it's awesome how quickly I could make it work
you can also do :
Copy code
modifier = Modifier
    .align(Alignment.BottomCenter)
    .offsetPercent(-0.30f)
    .size(100.dp)
    .background(Color.Red)
k
looks good, i’m checking. i will let you know if it worked. @nitrog42 thanks
n
btw you don't have to use a "custom" modifier, you can also just do :
Copy code
Box(
            modifier = Modifier
                .layout {
        measurable, constraints ->          
        val placeable = measurable.measure(constraints)
        return layout(placeable.width, placeable.height) {
            placeable.placeRelative(0, (constraints.maxHeight * 0.7f).roundToInt())
        }
                }
                .size(100.dp)
                .background(Color.Red)
        ) {
        }
👍 1
k
@nitrog42 actually i think it is using Parent View’s Size, can we change it to specific view/composable ?
n
I don't think we can
c
create a view with fillHeight(percentage), add your component inside and orient it to bottom
k
@joney Thanks, I am able to achieve it with
ConstraintLayout
👍 1