目前(1.x.y
),FloatingActionButton
不支持enabled
属性。
作为解决方法,您可以使用带有CircleShape
的Button
。
var enabled by remember { mutableStateOf(false) }
Button(
onClick = { },
modifier = Modifier.defaultMinSize(minWidth = 56.dp, minHeight = 56.dp),
enabled = enabled,
shape = CircleShape
){
Icon(Icons.Filled.Favorite, contentDescription = "Localized description")
}
如果你想使用一个FloatingActionButton
,可以像这样做:
var enabled by remember { mutableStateOf(false) }
CompositionLocalProvider(LocalRippleTheme provides
if (enabled) LocalRippleTheme.current else NoRippleTheme) {
FloatingActionButton(
backgroundColor = if (enabled) MaterialTheme.colors.secondary else Gray,
onClick = { if (enabled) { /* do something */ } else {} },
) {
Icon(Icons.Filled.Favorite,
contentDescription = "Localized description",
tint = if (enabled)
LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
else DarkGray)
}
}
与:
private object NoRippleTheme : RippleTheme {
@Composable
override fun defaultColor() = Color.Unspecified
@Composable
override fun rippleAlpha(): RippleAlpha = RippleAlpha(0.0f, 0.0f, 0.0f, 0.0f)
}
onClick = null
将禁用ExtendedFloatingActionButton
而不是FloatingActionButton
。 - Alpha 1