Jamie Craane
10/06/2020, 6:40 AMclass MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var deleted by mutableStateOf(false)
val x = mutableStateOf(0f)
val y = mutableStateOf(0f)
val colors = listOf(Color.Green, Color.Gray, Color.Blue)
setContent {
DragAndDropTestTheme {
Surface(color = MaterialTheme.colors.background) {
Column() {
// This row contains potential drop targets
Row(
modifier = Modifier
.height(150.dp)
.fillMaxWidth()) {
colors.map {
Box(
modifier = Modifier.background(it).weight(1f).fillMaxHeight()
)
}
}
Box(
modifier = Modifier.fillMaxWidth().fillMaxHeight()
.background(Color.Magenta)
) {
if (!deleted) {
Box(
modifier = Modifier
.offsetPx(x, y)
.background(Color.Cyan).width(75.dp).height(75.dp)
.dragGestureFilter(object : DragObserver {
override fun onDrag(dragDistance: Offset): Offset {
val newY = y.value + dragDistance.y
x.value = x.value + dragDistance.x
y.value = newY
return dragDistance
}
override fun onStop(velocity: Offset) {
// Todo what is the best method of determining what box the component is dragged on
}
})
) {
Text(text = "Dragme", modifier = Modifier.align(Alignment.Center))
}
}
}
}
}
}
}
}
}