关于
本篇主要记录一些开发中可能用到的常用方法的扩展记录,包括防快速带点击,画虚实线divider,画虚线边框,绘制阴影等。
防快速点击
inline fun Modifier.singleClickable(
debounceDuration: Long = 400L,
enabled: Boolean = true,//中间这三个是clickable自带的参数
rippleEnabled: Boolean = true, //是否开启水波纹
onClickLabel: String? = null,
role: Role? = null,
crossinline onClick: () -> Unit
): Modifier = composed {
var lastClickTime by remember {
mutableStateOf(value = 0L) }
val eventAction: () -> Unit = {
val currentTimeMillis = System.currentTimeMillis()
if (currentTimeMillis - debounceDuration >= lastClickTime) {
onClick()
lastClickTime = currentTimeMillis
}
}
if (rippleEnabled) {
clickable(enabled, onClickLabel, role, eventAction)
} else {
clickable(
interactionSource = NoRippleInteractionSource(),
indication = null,
enabled = enabled,
onClickLabel = onClickLabel,
role = role,
onClick = eventAction
)
}
}
class NoRippleInteractionSource : MutableInteractionSource {
override val interactions: Flow<Interaction> = emptyFlow()
override suspend fun emit(interaction: Interaction) {
}
override fun tryEmit(interaction: Interaction) = true
}