Hmm, why Icon with painter not changing on recompo...
# compose
i
Hmm, why Icon with painter not changing on recompose? I have mechanics to change dark theme. And line below works: icons changing when app theme change (used for example just random vectors)
Copy code
Icon(
    imageVector = with(Icons.Default) { if (isAppDarkTheme()) DateRange else KeyboardArrowRight },
    contentDescription = null
)
but if i replace it with resources icon not changing
Copy code
Icon(
    painter = painterResource(id = if (isAppDarkTheme()) R.drawable.ic_checkbox_on_night else R.drawable.ic_checkbox_on_day),
    contentDescription = null
)
Is it the bug or i should set vector to icon with another mechanism?
n
Is
isAppDarkTheme()
returning something that compose will know is changing? A
state
?
Maybe simplify this and pass a boolean into your composable function instead?
i
isAppDarkTheme()
returns boolean. Like this
n
It's probably going to calculate
isSystemInDarkTheme()
once here.
i
i toggle the
Copy code
darkLightThemeState
from DARK to LIGHT on button click. And if i use ImageVector instead painterResource icon changes
n
Your resources are set up with a drawable-night folder with the dark alternates in there?
i
nope.
isAppDarkTheme()
is just a boolean value, not dependent on system anyhow. So i just store resources in
drawable
folder
without any modifiers
n
Yeah, I scrolled back up and noticed you're just using the resource names. I'm out of ideas. 🙂
z
Are you actually getting different return values from
isAppDarkTheme
when you’re expecting it to change? If so, sounds like it might be a bug in
Icon
or
painterResource
. If that’s the case, figure out if you’re getting a new painter when the value changes, file a bug on the affected component, and you could probably work around it by wrapping your icon with the
key
composable.
n
can you try :
Copy code
Icon(
    painter = if (isAppDarkTheme()) painterResource(id = R.drawable.ic_checkbox_on_night) else painterResource(id = R.drawable.ic_checkbox_on_day),
    contentDescription = null
)
?
i
@nitrog42 tried it with same result: icon not changing. I’ll try to find a time to create an app to reproduce this for bug report if it is
z
so starting with your original code, i’d do something like this:
Copy code
println("resource: $res")
val painter = painterResource(id = res)
println("painter: $painter")
Icon(painter, …)
That will tell you if the painter is being updated correctly. If not, that’s probably a bug. If it is, then
Icon
probably has a bug. Either way, if that’s the issue, you should be able to workaround it by doing:
Copy code
key(res) {
  Icon(painter = painterResource(id = res), …)
}
i
Copy code
I/System.out: 2131099749
I/System.out: androidx.compose.ui.graphics.vector.VectorPainter@4d87db1
I/System.out: 2131099748
I/System.out: androidx.compose.ui.graphics.vector.VectorPainter@4d87db1
I/System.out: 2131099749
I/System.out: androidx.compose.ui.graphics.vector.VectorPainter@4d87db1
I/System.out: 2131099748
I/System.out: androidx.compose.ui.graphics.vector.VectorPainter@4d87db1
I/System.out: 2131099749
I/System.out: androidx.compose.ui.graphics.vector.VectorPainter@4d87db1
I/System.out: 2131099748
I/System.out: androidx.compose.ui.graphics.vector.VectorPainter@4d87db1
c
@Louis Pullen-Freilich [G] may have some insights
Does the same happen if you use
Image
instead of
Icon
?
i
Image works perfect, thanks! I am new in compose and somehow missed it. But this behavior for Icon... Is it normal?
l
What is the difference between
ic_checkbox_on_night
and
ic_checkbox_on_day
- is it just a different color?
Icon
by default applies a tint color, so the resource might be changing correctly, but
Icon
is just always applying a color on top that makes it look like it is the same
☝️ 1
👍🏼 1
i
Yes, only the color
l
If you use
Icon
, and set
tint = Color.Unspecified
- then this would probably work
But since you don’t want tint to be applied, which is the main point of
Icon
, probably easier just to use
Image
instead
👌🏼 1
👌 3
i
Thanks!