Skip to content

Instantly share code, notes, and snippets.

@rahulsainani
Last active November 1, 2021 11:45
Show Gist options
  • Save rahulsainani/fb16f7e8b951ff926ad7bc4650f4ccbb to your computer and use it in GitHub Desktop.
Save rahulsainani/fb16f7e8b951ff926ad7bc4650f4ccbb to your computer and use it in GitHub Desktop.
@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