Hello, I am trying to use `BadgedBox`, but the bad...
# compose-desktop
d
Hello, I am trying to use
BadgedBox
, but the badge is not placed correctly in my UI:
I call this code in a
CenterAlignedTopAppBar
, in its
actions
lambda, you can see the result at the bottom of the screenshot
a
Can you provide a short reproducer?
d
Copy code
@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun TestWindow() {
    Window(onCloseRequest = {}) {
        Scaffold(
            topBar = {
                CenterAlignedTopAppBar(
                    title = { Text("Badged") },
                    actions = {
                        BadgedBox(badge = { Badge { Text("box") } }) {
                            IconButton(onClick = {}) {
                                Icon(Icons.Default.Check, contentDescription = null)
                            }
                        }
                        IconButton(onClick = {}) {
                            Icon(Icons.Default.Close, contentDescription = null)
                        }
                    }
                )
            }
        ) {
            Text("Badged Box", modifier = Modifier.padding(it))
        }
    }
}
I added backgrounds on the
IconButton
and the
Text
a
Looks the same on Android:
Copy code
Box(Modifier.fillMaxSize()) {
        BadgedBox(
            modifier = Modifier.align(Alignment.Center).border(1.dp, Color.Black),
            badge = {
                Badge { Text("100") }
            }
        ) {
            IconButton(onClick = {}) {
                Icon(Icons.Default.Check, contentDescription = null)
            }
        }
    }
d
hmm, weird, it looks weird in the
TopAppBar
and I cannot make it draw over the
IconButton
not sure if I should move this to #compose asa a material3 bug/feature request
a
It looks the same in TopAppBar to me. Am I missing something?
I think the TopAppBar doesn’t take into account the size of the actions when sizing itself, and the default size of the icon button is too big.
👍🏻 1
Copy code
@OptIn(ExperimentalMaterial3Api::class)
fun main() = singleWindowApplication {
    CenterAlignedTopAppBar(
        title = {
            Text("Title")
        },
        actions = {
            BadgedBox(
                modifier = Modifier
                    .padding(horizontal = 24.dp)
                    .border(1.dp, Color.Black),
                badge = {
                    Badge { Text("100") }
                }
            ) {
                IconButton(
                    onClick = {},
                    modifier = Modifier.size(30.dp)
                ) {
                    Icon(Icons.Default.Check, contentDescription = null)
                }
            }
        }
    )
}
Whatever the size of the actions is, it just centers it vertically
And the badge also doesn’t seem to be taken into account when sizing BadgedBox
d
I also increased the font size of the badge's text, and it was drawn behind the window's decoration