Created
August 14, 2021 16:00
-
-
Save dhairya137/a1eb1e93eb6cc4507e6620fd116be1b3 to your computer and use it in GitHub Desktop.
Reusable Image card component made in Kotlin with jetpack compose.
This file contains hidden or 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
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
val painter = painterResource(id = R.drawable.mountains) | |
val description = "Mountains" | |
val title = "I am watching mountains" | |
Box(modifier = Modifier.fillMaxWidth(0.5f) | |
.padding(16.dp) | |
) { | |
ImageCard(painter = painter, contentDescription = description, title = title) | |
} | |
} | |
} | |
} | |
@Composable | |
fun ImageCard( | |
painter: Painter, | |
contentDescription: String, | |
title: String, | |
modifier: Modifier = Modifier | |
) { | |
Card( | |
modifier = modifier.fillMaxWidth(), | |
shape = RoundedCornerShape(15.dp), | |
elevation = 5.dp | |
) { | |
Box(modifier = Modifier.height(200.dp)) { | |
Image( | |
painter = painter, | |
contentDescription = contentDescription, | |
contentScale = ContentScale.Crop | |
) | |
Box(modifier = Modifier.fillMaxSize() | |
.background( | |
Brush.verticalGradient( | |
colors = listOf( | |
Color.Transparent, | |
Color.Black | |
), | |
startY = 300f | |
) | |
) | |
) | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(12.dp), | |
contentAlignment = Alignment.BottomCenter | |
) { | |
Text(title, style = TextStyle(color = Color.White, fontSize = 16.sp)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment