Jetpack Compose Support Pass in Composable with Modifier.align (BoxScope DSL)

December 15, 2021

Support pass in compsable with support Modifier.align

@Composable
fun PhotoItem(modifier: Modifier = Modifier, data: Any? = null, content: (@Composable BoxScope.() -> Unit)? = null) {
    val painter = rememberImagePainter(
        data = idata ?: R.drawable.error,
        builder = {
            // size(64)
            placeholder(R.drawable.placeholder)
            // transition when placeholder -> image
            crossfade(true)
            error(R.drawable.error)
        }
    )

    Box(modifier) {
        Image(
            painter = painter,
            contentDescription = null,
            modifier = Modifier.aspectRatio(1.0f), 
            contentScale = ContentScale.Crop
        )

        if (content != null) {
            content()
        }
    }
}

Usage

PhotoItem(data = "https://picsum.photos/id/237/200/300") {
  Text(modifier = Modifier.padding(8.dp).align(Alignment.BottomCenter, text = "Hello")
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.