Steve C
10/09/2021, 5:10 AMdata class MyBox(val index: Int, val color: Color)
@Composable
fun DraggingTest() {
val boxes = mutableListOf<MyBox>()
boxes.add(MyBox(0, Color.Blue))
boxes.add(MyBox(1, Color.Red))
boxes.add(MyBox(2, Color.Green))
ConstraintLayout(modifier = Modifier
.fillMaxSize()
.background(Brush.verticalGradient(listOf(Color.Black, Color.Transparent)))
) {
if(boxes.isNotEmpty()) {
DraggableBox(index = boxes[0].index, color = boxes[0].color, boxes.takeLast(boxes.size-1))
}
}
}
@Composable
fun DraggableBox(index: Int, color: Color, boxes: List<MyBox>) {
val initialYOffset = if(index == 0) 0f else 100f
Box(modifier = Modifier.fillMaxSize())
{
val offsetX = remember { mutableStateOf(0f) }
val offsetY = remember { mutableStateOf(initialYOffset) }
Box(modifier = Modifier
.offset { IntOffset(offsetX.value.roundToInt(), offsetY.value.roundToInt()) }
.size(150.dp).background(color)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
offsetX.value = (offsetX.value + dragAmount.x)
offsetY.value = (offsetY.value + dragAmount.y)
}
}
) {
Text("Box $index", modifier = Modifier.align(Alignment.BottomCenter)
.offset(0.dp, 20.dp))
if(boxes.isNotEmpty()) {
DraggableBox(index = boxes[0].index, color = boxes[0].color, boxes.takeLast(boxes.size-1))
}
}
}
}
Zach Klippenstein (he/him) [MOD]
10/12/2021, 2:08 AMSteve C
10/12/2021, 2:22 AM