HelloCW
HelloCW

Reputation: 2305

Which scope will LocalContentAlpha.current effect when I use Jetpack Compose?

I run Code A to display the same icon with different color, and Result A is the value of LocalContentAlpha.current. I guess the LocalContentAlpha.current which locate different position will return different value.

I guess that the LocalContentAlpha.current inside Log.e("my", "Enabled "+LocalContentAlpha.current.toString() ) is the scope of IconButton.

Could you tell me if the scope of LocalContentAlpha.current inside Log.e("my", "Outside "+LocalContentAlpha.current.toString() ) is Row() ?

Code 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))
}

Result 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

Upvotes: 0

Views: 254

Answers (1)

Abhimanyu
Abhimanyu

Reputation: 14857

You can find where the LocalContentAlpha value comes from by looking into the source code of the composables.

In IconButton you can find,

val contentAlpha = if (enabled) LocalContentAlpha.current else ContentAlpha.disabled
CompositionLocalProvider(LocalContentAlpha provides contentAlpha, content = content)

This is the source of LocalContentAlpha values in IconButton.
In Row, we cannot find any such usage of CompositionLocalProvider.
So you have to look at the parent composable to see if it provides a LocalContentAlpha.

Upvotes: 1

Related Questions