Home > Software engineering >  Draw a circle behind BadgeBox to make it more visible, circle getting too small
Draw a circle behind BadgeBox to make it more visible, circle getting too small

Time:07-19

My code:

IconButton(onClick = { /*TODO*/ }) {
    BadgedBox(
        modifier = Modifier.drawBehind
        {
            drawCircle(
                color = Color.LightGray,
                center = Offset(
                    this.size.maxDimension / 2,
                    this.size.maxDimension / 2
                ),
                radius = 10f
            )
        },
        badge = { Badge(modifier = Modifier.size(12.dp)) {} },
    ) {
        Icon(imageVector = Icons.Outlined.Notifications, contentDescription = "")
    }

Current results:

enter image description here

The light-gray circle, shall cover in background behind and look like a circular button. But, currently, it doesn't look like that. I tried with maxDimension. I am doing this in Kotlin with Jetpack Compose.

Desired:

enter image description here

EDIT: Added this line,

modifier = Modifier.clip(CircleShape).background(Color.LightGray)

Now it looks like this:

enter image description here

However, this looks very strange and does not provide the desired results.

CodePudding user response:

You can use something like:

    BadgedBox(
        badge = { Badge(modifier = Modifier.size(12.dp)) {} },
    ) {
        val radius = 16.dp
        val shape = RoundedCornerShape(radius)
        Box(
            contentAlignment=  Alignment.Center,
            modifier = Modifier
                .defaultMinSize(minWidth = radius * 2, minHeight = radius * 2)
                .background(
                    color = Color.LightGray,
                    shape =  shape
                )
                .clip(shape)
            ,
        ) {
            Icon(imageVector = Icons.Outlined.Notifications, contentDescription = "")
        }
    }

enter image description here

CodePudding user response:

You can do it using one box that covers both content and notification circle

@Composable
private fun MyBadgeBox(
    badge: @Composable () -> Unit,
    notificationRadius: Dp = 8.dp,
    notification: Boolean
) {

    Box {
        Box(modifier = Modifier.padding(notificationRadius)) {
            badge()
        }

        Box(
            modifier = if (notification) Modifier
                .padding(notificationRadius / 2)
                .align(Alignment.TopEnd)
                .size(notificationRadius * 2)
                .drawBehind {
                    drawCircle(Color.Red)
                } else Modifier)
    }
}

Usage

                val modifier = Modifier
                    .size(40.dp)
                    .background(
                        color = Color.LightGray.copy(.5f),
                        shape = CircleShape
                    )
                    .clip(CircleShape)
                    .clickable { }
                    .padding(6.dp)

                MyBadgeBox(
                    badge = {
                        Icon(
                            modifier = modifier,
                            imageVector = Icons.Outlined.Notifications,
                            contentDescription = ""
                        )

                    }, notification = false
                )

                MyBadgeBox(
                    badge = {
                        Icon(
                            modifier = modifier,
                            imageVector = Icons.Outlined.Notifications,
                            contentDescription = ""
                        )

                    }, notification = true
                )
            }

Result

enter image description here

CodePudding user response:

please try:

fun Modifier.rightRead(read: Boolean, badgeColor: Color) = this
    .drawWithContent {
        drawContent()
        if (!read) {
            drawCircle(
                color = badgeColor,
                radius = 5.dp.toPx(),
                center = Offset(size.width - 1.dp.toPx(), 1.dp.toPx()),
            )
        }
}

Adjust his offset according to the image size

example:

Image(...,Modifier.rightReadCircle(false, Color.Red))
  • Related