Last active
August 21, 2023 19:53
-
-
Save mo0rti/260c35752a3718d54e774ab3ccbc0179 to your computer and use it in GitHub Desktop.
Custom list item to display in different previews
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
import android.content.res.Configuration | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.layout.Arrangement | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.Spacer | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.foundation.layout.width | |
import androidx.compose.material3.Checkbox | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.graphicsLayer | |
import androidx.compose.ui.graphics.painter.Painter | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.platform.LocalConfiguration | |
import androidx.compose.ui.text.style.TextDecoration | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun LandscapeListItem( | |
imageResource: Painter, | |
text: String, | |
isChecked: Boolean, | |
onCheckedChange: (Boolean) -> Unit | |
) { | |
Row( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(16.dp), | |
horizontalArrangement = Arrangement.SpaceBetween, | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
// Column for Image and Text | |
Column( | |
modifier = Modifier.weight(1f), | |
verticalArrangement = Arrangement.Center, | |
horizontalAlignment = Alignment.Start | |
) { | |
// Image | |
Image( | |
painter = imageResource, | |
contentDescription = null, | |
contentScale = ContentScale.Crop, | |
modifier = Modifier | |
.height(48.dp) | |
.graphicsLayer { | |
// Apply blur if checkbox is checked | |
alpha = if (isChecked) 0.3f else 1f | |
} | |
) | |
Spacer(modifier = Modifier.height(8.dp)) | |
// Text | |
Text( | |
text = text, | |
textDecoration = if (isChecked) TextDecoration.LineThrough else TextDecoration.None | |
) | |
} | |
// Checkbox | |
Checkbox( | |
checked = isChecked, | |
onCheckedChange = onCheckedChange | |
) | |
} | |
} | |
@Composable | |
fun PortraitListItem( | |
imageResource: Painter, | |
text: String, | |
isChecked: Boolean, | |
onCheckedChange: (Boolean) -> Unit | |
) { | |
Row( | |
modifier = Modifier | |
.fillMaxWidth() | |
.padding(16.dp), | |
horizontalArrangement = Arrangement.SpaceBetween, | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
// Image | |
Image( | |
painter = imageResource, | |
contentDescription = null, | |
contentScale = ContentScale.Crop, | |
modifier = Modifier | |
.size(48.dp) | |
.graphicsLayer { | |
// Apply blur if checkbox is checked | |
alpha = if (isChecked) 0.3f else 1f | |
} | |
) | |
Spacer(modifier = Modifier.width(8.dp)) | |
// Text | |
Text( | |
text = text, | |
modifier = Modifier.weight(1f), | |
textDecoration = if (isChecked) TextDecoration.LineThrough else TextDecoration.None | |
) | |
Spacer(modifier = Modifier.width(8.dp)) | |
// Checkbox | |
Checkbox( | |
checked = isChecked, | |
onCheckedChange = onCheckedChange | |
) | |
} | |
} | |
@Composable | |
fun AdaptiveListItem( | |
imageResource: Painter, | |
text: String, | |
isChecked: Boolean, | |
onCheckedChange: (Boolean) -> Unit | |
) { | |
val configuration = LocalConfiguration.current | |
if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) { | |
LandscapeListItem(imageResource, text, isChecked, onCheckedChange) | |
} else { | |
PortraitListItem(imageResource, text, isChecked, onCheckedChange) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment