Last active
November 1, 2021 11:45
-
-
Save rahulsainani/fb16f7e8b951ff926ad7bc4650f4ccbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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