`Row` doesn't want to get recomposed even after hi...
# compose
g
Row
doesn't want to get recomposed even after his child
Text
become bigger. Should I recompose it manually? If yes, how can I do it?
a
That sounds like a bug, doesn't it?
g
I think it's an optimisation. But looks like a bug
a
can you describe the case a bit more? What caused the
Text
to become bigger? Did the content of the
Text
change or something else?
I ask because layout and composition are different processes that invalidate differently
it is quite valid and expected for a
Row
to not recompose if the layout size of one of its children changes. It will relayout, but not recompose.
g
I replace text in
Text
with more symbols
a
Can you post the code in question?
g
Ok, I will try to create the test one
πŸ‘ 1
Copy code
@OptIn(ExperimentalAnimationApi::class)
@Preview
@Composable
fun MyTest() {
    var text by remember { mutableStateOf("0") }
    var visible by remember { mutableStateOf(false) }
    TestTheme {
        Row(modifier = Modifier.fillMaxWidth().height(30.dp)) {
            Spacer(
                modifier = Modifier.weight(1f).background(Color.Red).height(30.dp).clickable(
                    onClick = {
                        visible = true
                    }
                )
            )
            AnimatedVisibility(visible = visible) {
                Text(text, modifier = Modifier.clickable {
                    text += "0"
                })
            }
        }
    }
}
I finally finished. That was much harder that I thought
I figured out that if
visible
init value is
true
, all work fine, but if it is
false
and than we set it to true, it doesn't work fine
a
silly question, what is, "fine" in this context? Based on reading the code above I would expect the contents of the
AnimatedVisibility
to recompose for the new text, and the row to remeasure+relayout for the changed text content, but not recompose the row.
Is relayout not happening correctly?
g
Yes, after clicking on
Text
Spacer
remains the same size
a
hmm, @Doris Liu @George Mount maybe something in
AnimatedVisibility
?
g
It's definitely a bug, because it has different behaviour with same "state"
Switch visibility to
false
and back to
true
must not change behaviour
I hope you understand what I'm trying to say
d
This does seem like a bug in
AnimatedVisibility
. Could you file a bug please?
πŸ‘Œ 1
Out of curiosity, do you expect the size change (from text change) to be animated in
AnimatedVisibilty
?
g
I do not
d
Cool. Me neither. πŸ™‚
g
But know I wonder how to do it πŸ™‚
d
I'd suggest
Modifier.animateContentSize()
for that purpose. πŸ™‚
πŸ™ 1
More specifically:
Copy code
Text(text, modifier = Modifier.clickable {
                    text += "0"
                }.animateContentSize() )
h
Hi @Doris Liu, I wanted to animated my Text composable, so stumbled upon this thread. I the video below I am using PropKeys for alpha, size, color of components, but for text I’m using
Modifier.animateContentSize()
The problem I am facing is that the text is changed first and then the size changes, see the transition from Added to Add. Is this the intended behaviour? What’s the best way to achieve this interaction? Can I use PropKeys here?
d
Yes. The intended behavior is to change the content (in this case the text) immediately and animate the container size afterwards. Is there a specific effect you are hoping to achieve?
The best way to achieve this interaction is to use propkeys for the colors, and alpha, and perhaps a custom
AnimationSpec
in
animateContentSize
if you need to coordinate them. Alternatively, you could consider animating the check mark in
AnimatedVisibility
using fade and expand/shrink to add a little more dynamic to the animation. πŸ™‚
h
Yes, I have used propKeys for color and alpha, what I want is that when we unselect the button, it animates smoothly but right now it is first changing ADDED to ADD and then animating it. I want ADDED to smoothly animate to ADD. Basically reverse of when the above interaction goes from ADD to ADDED.
Great suggestion on check mark animation, will surely try that too πŸ™‚
The intended behavior is to change the content (in this case the text) immediately and animate the container size afterwards.
In my case I may want the content to change afterwards, is this achievable? or any other way to achieve this interaction?
d
The trouble is we don't know the target size to animate to without changing the text. We might have a different API to support that going forward. For now, here's an unconventional idea: put "ED" in a separate text and animate that in/out using
AnimatedVisibility
πŸ˜‚
h
Hahaha, just tried this, works like a charm πŸ˜„
πŸ‘ 1