Skip to content

Instantly share code, notes, and snippets.

@rahulsainani
Last active Nov 1, 2021
Embed
What would you like to do?
@Composable
private fun PhotosRow(
images: List<BeautifulCreature>,
numPhotosVisible: Float,
modifier: Modifier = Modifier,
) {
BoxWithConstraints(modifier = modifier) {
// Arbitrarily chosen 20 as number of "units" to divide the available width
val numGrids = 20
// Using available space to calculate the space between items and itemWidth
val spaceBetweenItems = maxWidth.div(numGrids)
val itemWidth = (maxWidth - spaceBetweenItems).div(numPhotosVisible)
LazyRow {
items(images) {
PhotoCard(
onClick = { },
photo = it.photo,
contentDescription = it.name,
modifier = Modifier
.width(itemWidth)
.aspectRatio(1f)
)
if (it != images.last()) {
Spacer(modifier = Modifier.width(spaceBetweenItems))
}
}
}
}
}
@Composable
fun PhotoCard(
onClick: () -> Unit,
photo: String,
contentDescription: String,
modifier: Modifier = Modifier,
) {
Card(
onClick = onClick,
modifier = modifier
) {
Image(
painter = rememberImagePainter(data = photo),
contentDescription = contentDescription,
contentScale = ContentScale.Crop
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment