Has anyone ever had a Switch that wasn't controlle...
# compose
n
Has anyone ever had a Switch that wasn't controlled by animation?
Copy code
val normalModifier = 0.dp
val expandModifier = 700.dp

val animatedHeight by animateDpAsState(
    targetValue = (if(viewModel.textListSwitch) expandModifier else normalModifier),
    animationSpec = tween(500)
)
I wrote a height animation of the expanded content, but I found that the Text control inside can be hidden in this way, while the Switch control is still displayed
d
Can you share how this snippet is used with
Switch
and maybe a video clip of what the animation looks like now. 🙂
👍 2
n
yes sure
its my code
I seem to have solved the problem and when I use LazyColumn, the problem does not occur
Copy code
Column(
    modifier = Modifier
        .padding(8.dp)
        .height(animatedHeight)
        .verticalScroll(rememberScrollState())
)
what, I also found that if I added .verticalScroll, it also became normal
d
The Switch has a
requiredSize
defined: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]124?q=switch.kt&ss=androidx%2Fplatform%2Fframeworks%2Fsupport As a result, when the parent doesn't have enough space (e.g. 0 height) to accommodate the Switch, it will be centered in the parent. Since the parent doesn't clip, you end up seeing the whole switch. Here is an example of the
requiredSize
modifier if you are curious.
👍 1
n
thank you !
d
For animating expanding/collapsing, I'd recommend
Modifier.animateContentSize
or
AnimatedVisibility
You could add
Modifier.animateContentSize
to your column:
Copy code
Column(Modifier.animateContentSize()) { 
if(viewModel.textListSwitch) // Show the content only when condition is met
}
Or put the Column in an
AnimatedVisibility
:
Copy code
AnimatedVisibility(visible = viewModel.textListSwitch) { Column(..) {...} }
n
thanks ! i will try it
by the way, is the navigate animation in Navigation going to be adapted yet? hhahaha
d
Navigation animation work is being tracked here: https://issuetracker.google.com/172112072 Please feel free to star for updates. 🙂
n
ok!!!