[英]Which scope will LocalContentAlpha.current effect when I use Jetpack Compose?
我运行代码 A 以显示具有不同颜色的相同图标,结果 A 是LocalContentAlpha.current
的值。 我猜定位不同 position 的LocalContentAlpha.current
将返回不同的值。
我猜Log.e("my", "Enabled "+LocalContentAlpha.current.toString() )
中的LocalContentAlpha.current
是 IconButton 的IconButton
。
你能告诉我Log.e("my", "Outside "+LocalContentAlpha.current.toString() )
内LocalContentAlpha.current
的 scope 是否是Row()
吗?
代码 A
@Composable
fun RecordTitleWithToolBar(
){
Row() {
Log.e("my", "Outside "+LocalContentAlpha.current.toString() )
IconButton(
enabled = true,
onClick = {}
) {
DisplayIcon(icon=Icons.Filled.PlayArrow)
Log.e("my", "Enabled "+LocalContentAlpha.current.toString() )
}
IconButton(
enabled = false,
onClick = {}
) {
DisplayIcon(icon=Icons.Filled.PlayArrow)
Log.e("my", "Disabled "+LocalContentAlpha.current.toString() )
}
}
}
@Composable
fun DisplayIcon(
icon: ImageVector,
dim: Dp = 24.dp,
tint: Color = Color.Blue
) {
Icon(icon, null, modifier = Modifier.size(dim), tint = tint.copy(LocalContentAlpha.current))
}
结果 A
2022-08-28 21:02:59.384 26068-26068/info.dodata.soundmeter E/my: Enabled 0.87
2022-08-28 21:02:59.387 26068-26068/info.dodata.soundmeter E/my: Disabled 0.38
2022-08-28 21:02:59.466 26068-26068/info.dodata.soundmeter E/my: Outside 0.87
2022-08-28 21:02:59.471 26068-26068/info.dodata.soundmeter E/my: Enabled 0.87
2022-08-28 21:02:59.475 26068-26068/info.dodata.soundmeter E/my: Disabled 0.38
2022-08-28 21:02:59.480 26068-26068/info.dodata.soundmeter E/my: Outside 0.87
2022-08-28 21:02:59.485 26068-26068/info.dodata.soundmeter E/my: Enabled 0.87
2022-08-28 21:02:59.487 26068-26068/info.dodata.soundmeter E/my: Disabled 0.38
您可以通过查看可组合项的源代码来找到LocalContentAlpha
值的来源。
在IconButton
你可以找到,
val contentAlpha = if (enabled) LocalContentAlpha.current else ContentAlpha.disabled
CompositionLocalProvider(LocalContentAlpha provides contentAlpha, content = content)
这是IconButton
中LocalContentAlpha
值的来源。
在Row
中,我们找不到CompositionLocalProvider
的任何此类用法。
因此,您必须查看父可组合以查看它是否提供LocalContentAlpha
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.