package
com.geeksforgeeks.jcpointerinputmodifier
import
android.os.Bundle
import
androidx.activity.ComponentActivity
import
androidx.activity.compose.setContent
import
androidx.compose.foundation.background
import
androidx.compose.foundation.gestures.detectDragGestures
import
androidx.compose.foundation.layout.*
import
androidx.compose.material.*
import
androidx.compose.runtime.getValue
import
androidx.compose.runtime.mutableStateOf
import
androidx.compose.runtime.remember
import
androidx.compose.runtime.setValue
import
androidx.compose.ui.Alignment
import
androidx.compose.ui.Modifier
import
androidx.compose.ui.graphics.Color
import
androidx.compose.ui.input.pointer.consumeAllChanges
import
androidx.compose.ui.input.pointer.pointerInput
import
androidx.compose.ui.unit.IntOffset
import
androidx.compose.ui.unit.dp
import
kotlin.math.roundToInt
class
MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContent {
Scaffold(
topBar = { TopAppBar(title = { Text(
"GFG | Pointer Input Modifier"
, color = Color.White) }, backgroundColor = Color(
0xff0f9d58
)) },
content = {
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
Box(
Modifier
.offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
.background(Color.Blue)
.size(
50
.dp)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
offsetX += dragAmount.x
offsetY += dragAmount.y
}
}
)
}
}
)
}
}
}