Alexandru Gheorghe
06/03/2024, 12:44 PMLazyColumn {
items(1) {
Column(
modifier = modifier
.fillMaxWidth(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
UploadImageBox(
modifier = Modifier,
imageViewModel = imageViewModel,
onImageSelected = {}
)
}
// other 3 Column() composables
}
}
where UploadImageBox
is:
@Composable
fun UploadImageBox(
modifier: Modifier = Modifier,
imageViewModel: ImageViewModel,
onImageSelected: (Uri) -> Unit
) {
val context = LocalContext.current
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.GetContent(),
onResult = { uri ->
if (uri != null) {
imageViewModel.updateImageUri(uri)
onImageSelected(uri)
}
}
)
Box(
modifier = Modifier
.clickable { launcher.launch("image/*") }
) {
Column(
modifier = modifier
.padding(
dimensionResource(
R.dimen.padding
)
),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
if (imageViewModel.imageUri.value != null) {
/** Show selected image */
Image(
painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(context)
.data(imageViewModel.imageUri.value)
.crossfade(true)
.build()
),
contentDescription = stringResource(
R.string.selected_image_description
),
modifier = Modifier
.clip(RoundedCornerShape(
dimensionResource(R.dimen.corner_size)
))
)
} else {
/** Show placeholder icon */
Icon(
Icons.Filled.Add,
contentDescription = stringResource(
R.string.select_image_description
),
modifier = Modifier
.size(dimensionResource(icon_size))
)
}
}
}
}
the viewmodel is simple:
private val _imageUri = mutableStateOf<Uri?>(null)
val imageUri = _imageUri
fun updateImageUri(imageUri: Uri) {
_imageUri.value = imageUri
}
however, when I select the add icon to add an image, the image disappears from the screen on the android phone after being selected (the plus sign is no longer available either which means the image was read but not displayed) -- screenshots in thread. What am I doing wrong?Alexandru Gheorghe
06/03/2024, 12:46 PMAlexandru Gheorghe
06/03/2024, 12:47 PMAlexandru Gheorghe
06/03/2024, 12:48 PMAlexandru Gheorghe
06/03/2024, 12:50 PMColumn
instead of LazyColumn
then it works. Though, I want the user to be able to scroll the screen.Alexandru Gheorghe
06/03/2024, 1:15 PMheight
on the modifier for image for coil to renderColton Idle
06/03/2024, 2:19 PM