0

The library I'm using: "com.google.accompanist:accompanist-placeholder-material:0.23.1"

I want to display a placeholder in the place of (or over) a component when it's in the loading state.

I do the following for a Text:

MaterialTheme() {
    var placeholderVisible by remember { mutableStateOf(false) }
    LaunchedEffect(Unit) {
        while (true) {
            delay(1000)
            placeholderVisible = !placeholderVisible
        }
    }
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Box(
            modifier = Modifier
                .border(1.dp, Color.Red)
                .padding(16.dp)
        ) {
            Text(
                modifier = Modifier
                    .then(
                        if (placeholderVisible) {
                            Modifier.height(28.dp).width(62.dp)
                        } else {
                            Modifier
                        }
                    )
                    .placeholder(
                        visible = placeholderVisible,
                        highlight = PlaceholderHighlight.shimmer()
                    ),
                text = if (placeholderVisible) "" else "Hello"
            )
        }
    }
}

And I get this:

enter image description here

I want instead that no matter how big I set the placeholder's height or width, it will not participate in any way in the measuring process and, if I want to, to be able to draw itself even over other components (in this case let's say the red border).

As an effect of what I want, the box with red border will always have the dimension as if that Modifier.height(28.dp).width(62.dp) is not there.

I know I can draw outside a component's borders using drawWithContent, specifying the size of a rectangle or a circle (or whatever) to be component's size + x.dp.toPx() (or something like that). But how do I do this with Modifier.placeholder?

Ideally, I would need something like Modifier.placeholder(height = 28.dp, width = 62.dp)

So, with or without this ideal Modifier, the UI should never change (except, of course, the shimmer box that may be present or not).

I think I can pull this off by modifying the source code of this Modifier, but I hope I won't need to turn to that.

daniyelp
  • 915
  • 1
  • 11
  • 26

1 Answers1

0

Just replace your Text() with below code, maybe conditional Modifier is the issue in above code!

        Text(
            modifier = Modifier
                .size(width = 62.dp, height = 28.dp)
                .placeholder(
                    visible = placeholderVisible,
                    highlight = PlaceholderHighlight.shimmer()
                ),
            text = if (placeholderVisible) "" else "Hello",
            textAlign = TextAlign.Center
        )

enter image description here

Megh
  • 831
  • 2
  • 12
  • Common... It's not that simple. I don't want to set the the height of the text, but of the placeholder. – daniyelp Jul 26 '22 at 08:52