Hi there, I'm trying to use `accompanist-insets` t...
# compose
c
Hi there, I'm trying to use 
accompanist-insets
 to adjust my view size according to the height of the keyboard but when the insets finish animating, they reset to 0. Anyone seen this behavior? Any ideas on how I could fix it? I added a green box that displays the value of 
LocalWindowInsets.current.ime.bottom
 to show it is going back to 0 at the end of each animation.
1
🙏 1
b
I solved it by animating between
0.dp
and
LocalWindowInsets.current.ime.bottom
depending on keyboard visibility given by
LocalWindowInsets.current.ime.isVisible
. Like this:
Copy code
@Composable
fun KeyboardSpacer(
    modifier: Modifier = Modifier,
    confirmHeight: (Dp) -> Dp = { it },
) {
    val imeVisible = LocalWindowInsets.current.ime.isVisible
    val imeHeight = with(LocalDensity.current) { LocalWindowInsets.current.ime.bottom.toDp() }
    val height by animateDpAsState(if (imeVisible) confirmHeight(imeHeight) else 0.dp)
    Spacer(modifier.height(height))
}
z
That looks great @Berkeli Alashov! Any particular reason why youre doing it "yourself" as compared to just using one of the built in methods? Im thinking of
Modifier.imePadding()
which seems to acheive the exact same thing?
c
@Zoltan Demant I'm not sure if it is the same for @Berkeli Alashov as for me, but I'm using the accompanist
Modifier.imePadding()
and it is causing the issue
👍🏽 1
Unless @Berkeli Alashov is recreating the functionality of accompanist insets, the
LocalWindowInsets.current.ime.isVisible
shows that he is using it
b
@Zoltan Demant I needed control over how much of keyboard height I needed to use as padding (i.e I needed only half of keyboard height in that particular screen).
Modifier.imePadding()
doesn't allow changing how much of it to apply and using
LocalWindowInsets.current.ime.bottom
by itself causes a flicker at the start of hide animation. @clark it's odd that you're getting zero from
LocalWindowInsets.current.ime.bottom
at the end of animation, I just checked it myself and it's not happening on my end..
👍🏽 1
c
Not sure it's quite the same thing, but this works well for me to smoothly hide components when the keyboard is made visible:
Copy code
@Composable
fun HideFromIme(content: @Composable () -> Unit) {
  AnimatedVisibility(
    visible = !LocalWindowInsets.current.ime.isVisible,
    enter = expandVertically(),
    exit = shrinkVertically(),
  ) {
    content()
  }
}
c
I figured it out! I had a
NavHost
containing several screens that were all using
ProvideWindowInsets
. I took it out of the base screen they share and put the
ProvideWindowInsets
around the
NavHost
instead and the issue disappeared. Thanks for the ideas everyone!
👍🏽 1