Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use `AnimatedVisibility` in a `BoxScope`?

I have a layout which looks like this:

Row {
        ...

        Box(
            modifier = Modifier
                .fillMaxHeight()
                .width(50.dp)
        ) {
            AnimatedVisibility(
                visible = isSelected && selectedAnimationFinished,
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                ...
            }
        }
    }

But I get the compile-time error:

fun RowScope.AnimatedVisibility(visible: Boolean, modifier: Modifier = ..., enter: EnterTransition = ..., exit: ExitTransition = ..., content: AnimatedVisibilityScope.() -> Unit): Unit' can't be called in this context by implicit receiver. Use the explicit one if necessary

It appears that Kotlin finds the AnimatedVisibility function ambiguous, since Compose exposes multiple AnimatedVisibility functions with the same signature: there's a fun AnimatedVisibility with no receiver, and a fun RowScope.AnimatedVisibility which requires RowScope.

From what I can gather, Kotlin is complaining about me using the RowScope version incorrectly, but I just want to use the version with no receiver!

Using this.AnimatedVisibility also doesn't help.

The only workaround I've found that works is to fully qualify the name, like androidx.compose.animation.AnimatedVisibility(...). But I have no idea why this works.

Can anyone shed some light on this? Is there a better option I can use than fully qualifying the name?

like image 611
Luke Avatar asked Jun 14 '21 18:06

Luke


Video Answer


3 Answers

One workaround is to use a fully qualified name:

Box {
    androidx.compose.animation.AnimatedVisibility(visibile = ...) {
        ...
    }
}
like image 103
Alex Perevozchykov Avatar answered Oct 25 '22 03:10

Alex Perevozchykov


Looks like it's a bug in the language - overload resolution is not aware of @DslMarkers and such stuff. I couldn't find related issues on Kotlin bugtracker so I filed one myself - https://youtrack.jetbrains.com/issue/KT-48215.

like image 38
Jeffset Avatar answered Oct 25 '22 04:10

Jeffset


Another workaround is creating new composable method and using it in a Row:

@Composable
fun AnimatedThings() {
    Box {
        AnimatedVisiblity(visible = ...) {
            ...
        }
     }
 }
like image 1
Cemal Avatar answered Oct 25 '22 03:10

Cemal