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)
}
}
}
}
Compose中如何自定义Indication
最新推荐文章于 2025-03-20 20:58:17 发布