class BoxDemo : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
CompositionLocalProvider(LocalIndication provides Indication01) {
Box(
modifier = Modifier
.width(100.dp)
.clickable { }
.height(100.dp)
){
Text(text = "hello hi")
}
}
}
}
}
private object Indication01 : Indication {
private class DefaultDebugIndicationInstance(
private val isPressed: State<Boolean>,
private val isHovered: State<Boolean>,
private val isFocused: State<Boolean>,
) : IndicationInstance {
override fun ContentDrawScope.drawIndication() {
if (isPressed.value) {
drawRect(color = Color.Blue.copy(alpha = 1.0f), size = size)
} else if (isHovered.value || isFocused.value) {
drawRect(color = Color.Green.copy(alpha = 1.0f), size = size)
}
drawContent()
}
}
@Composable
override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
val isPressed = interactionSource.collectIsPressedAsState()
val isHovered = interactionSource.collectIsHoveredAsState()
val isFocused = interactionSource.collectIsFocusedAsState()
return remember(interactionSource) {
DefaultDebugIndicationInstance(isPressed, isHovered, isFocused)
}
}
}
}
12-23
663
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-23