https://kotlinlang.org logo
#compose
Title
# compose
a

Arun Joseph

01/24/2022, 7:39 PM
Can we somehow simplify this?
Copy code
if (enabled) {
                    Icon(
                        painter = painterResource(id = iconId),
                        contentDescription = null,
                        modifier = Modifier.size(32.dp),
                        tint = iconColor
                    )
                } else {
                    Icon(
                        painter = painterResource(id = iconId),
                        contentDescription = null,
                        modifier = Modifier.size(32.dp),
                    )
                }
I am trying something along these lines
Copy code
Icon(
                        painter = painterResource(id = iconId),
                        contentDescription = null,
                        modifier = Modifier.size(32.dp),
                        tint = if (enabled) iconColor else ...
                    )
m

mkrussel

01/24/2022, 7:44 PM
You can copy the default value from
Icon
for tint for the else part.
Copy code
LocalContentColor.current.copy(alpha = LocalContentAlpha.current)
a

Arun Joseph

01/24/2022, 7:46 PM
That's possible. But my code has to change if that default value changes in future. Probably this is more of a kotlin question
if (enabled) myvalue else [default value whatever it is]
j

Jan Bína

01/24/2022, 8:34 PM
But my code has to change if that default value changes in future.
That doesn't make much sense. Changing the default value would be breaking change so I wouldn't expect that happening. And if it will change, having it overriden is actually better for you, because your ui stays the same which is probably intended... Anyway, if you really want it that way, your best option now is to create your own Icon composable with nullable tintColor which will call the default Icon with or without tintColor...
a

Arun Joseph

01/24/2022, 8:40 PM
That doesn't make much sense. Changing the default value would be breaking change so I wouldn't expect that happening. And if it will change, having it overriden is actually better for you, because your ui stays the same which is probably intended...
This is a good point. Probably i was more thinking why to find the default color from library and put it in my code.
I was also thinking of putting
Copy code
else Color.Unspecified,
but that looks like taking a different path than
Copy code
LocalContentColor.current.copy(alpha = LocalContentAlpha.current)