Matti MK
10/19/2021, 9:16 AMButtonColors
based on the enabled
property value?
I have a button for which I’ve defined the disabled and enabled colors as shown below. How can I animate the color change when the enabled
property changes?
colors = ButtonDefaults.buttonColors(
backgroundColor = colors.primaryVariant,
disabledBackgroundColor = colors.secondaryVariant
),
gildor
10/19/2021, 9:23 AMgildor
10/19/2021, 9:23 AMMatti MK
10/19/2021, 9:24 AMMatti MK
10/19/2021, 9:25 AMgildor
10/19/2021, 9:29 AMMatti MK
10/19/2021, 9:31 AMMatti MK
10/19/2021, 9:31 AMgildor
10/19/2021, 9:32 AMMatti MK
10/19/2021, 9:50 AMdisabledBackgroundColor
from my theme, so declaring it in the ButtonDefaults.buttonColors
is simply overriding it: as such, changing the backgroundColor
by the enabled
state of the button doesn’t really do anything: backgroundColor
is not shown when the button is disabled
.
Not really feeling like re-creating the button myself, so I guess I’ll go without any state animation now 🤔gildor
10/19/2021, 9:52 AMgildor
10/19/2021, 9:52 AMTobias Suchalla
10/19/2021, 9:53 AMdisabledBackgroundColor
from backgroundColor
to actual disabled color.gildor
10/19/2021, 9:55 AMTobias Suchalla
10/19/2021, 9:55 AMval disabledColor: Color by animateColorAsState(if (enabled) colors.primaryVariant else colors.secondaryVariant)
...
colors = ButtonDefaults.buttonColors(
backgroundColor = colors.primaryVariant,
disabledBackgroundColor = disabledColor
)
Matti MK
10/19/2021, 10:00 AMval color by animateColorAsState(if (enabled) colors.primaryVariant else colors.secondaryVariant)
Button(
colors = ButtonDefaults.buttonColors(
backgroundColor = color,
disabledBackgroundColor = color
),
Just need to animate both props to get the animation both ways 👍
Thanks both of you for the help, much appreciated 🙇Louis Pullen-Freilich [G]
10/19/2021, 11:50 AMButtonColors
implementation, instead of using the default here. It would also allow expressing exactly what you want to instead of needing this hack to set both backgroundColor and disabledBackgroundColor to the same value 🙂Daniele Segato
10/19/2021, 11:53 AMLouis Pullen-Freilich [G]
10/19/2021, 11:56 AMMatti MK
10/19/2021, 11:57 AManimated-selector
Louis Pullen-Freilich [G]
10/19/2021, 11:59 AMDaniele Segato
10/19/2021, 12:01 PMButtonDefaults.animatedButtonColors()
that implement that tho'.
If the animation is quick enough I believe the "immediate feedback" would be acceptableLouis Pullen-Freilich [G]
10/19/2021, 12:05 PMMatti MK
10/20/2021, 5:59 AMTobias Suchalla
10/20/2021, 7:24 AM