Hi, I have two questions about my code. 1. at the ...
# compose
n
Hi, I have two questions about my code. 1. at the end, when I measure the x offset of my Tile, how do I reference the maxWidth of my parent Box? 2. how can I make sure that each Tile is dragged individually rather than as a group? 🧵
Copy code
@Composable
fun GameBoard() {
    val coroutineScope = rememberCoroutineScope()
    val offsetX = remember { Animatable(0f) }
    val offsetY = remember { Animatable(0f) }

    val tileModifier = Modifier
        .offset {
            IntOffset(
                offsetX.value.roundToInt(),
                offsetY.value.roundToInt()
            )
        }
        .draggable(
            state = rememberDraggableState { delta ->
                coroutineScope.launch {
                    offsetX.snapTo(offsetX.value + delta)
                }
            },
            orientation = Orientation.Horizontal,
            onDragStarted = {},
            onDragStopped = {
                coroutineScope.launch {
                    offsetX.animateTo(
                        targetValue = 0f,
                        animationSpec = tween(
                            durationMillis = 1000,
                            delayMillis = 0
                        )
                    )
                }
            }
        )
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .aspectRatio(0.75f)
            .padding(20.dp)
            .background(Color.DarkGray)
            .clipToBounds()

    ) {
        CustomLayout {
            for (i in 0..6) {
                Tile(color = Color.Magenta, tileModifier)
            }
            for (i in 7..13) {
                Tile(color = Color.Yellow, Modifier)
            }
        }
        println(offsetX.value)
        if (offsetX.value >= maxWidth of parent Box){

    }
    }
}
e
You can use BoxWithConstraints to get its maxWidth
n
Thanks but I have a Custom Layout so everything is already measured there.
a
Manage any offsets in the custom layout, not with offset modifiers on the children. Let the parent own the measurement and positioning of its children, that's its job.
🙏 1
n
Ah ok, I misunderstood you last time, I thought you meant manage the offsets in the GameBoard composable. I am gradually beginning to understand how it all works. 😅
a
This is one thing that hasn't changed much from Views at all 🙂 giving the parent sole ownership of child positioning always made things a lot easier there too; it's why things like ScrollView, DrawerLayout and CoordinatorLayout are set up the same way: a parent container that is responsible for changing the way children are measured and positioned and how user input influences that layout
👍 1