Android编写对话框仅打开一次

vptzau2j  于 2023-03-21  发布在  Android
关注(0)|答案(1)|浏览(140)

我已经开始在我的项目中使用合成。我面临着问题,不知道如何解决它。我有一个列表的照片。当我点击照片,我显示在全屏与此照片对话框。在照片上,我有关闭图标。当我点击关闭图标,对话框关闭。但如果我再次想显示此对话框,它不显示。我的代码:

@Composable
fun AnimalImagies(photoUrl: String) {
   var dialogOpen by remember {
        mutableStateOf(false)
    }

    if(dialogOpen){
        GetBifImage(photoUrl = photoUrl)
    }
    val imageLoader = LocalContext.current.imageLoader.newBuilder()
        .logger(DebugLogger())
        .build()
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data(photoUrl)
            .crossfade(true)
            .build(),
        imageLoader = imageLoader,
        contentDescription = "",
        contentScale = ContentScale.Crop,
        modifier = Modifier
            .size(140.dp)
            .clip(RoundedCornerShape(corner = CornerSize(16.dp)))
            .clickable(onClick = { dialogOpen = true })
    )
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun GetBifImage(photoUrl: String) {
    val shouldShowDialog = remember { mutableStateOf(true) }
    if (shouldShowDialog.value) {
        Dialog(
            properties = DialogProperties(usePlatformDefaultWidth = false),
            onDismissRequest = {shouldShowDialog.value = false}
        ) {
            Box(modifier = Modifier.fillMaxSize()) {
                ZoomImage(photoUrl)
                Image(painter = painterResource(id = R.drawable.ic_close_),
                    contentDescription = "",
                    modifier = Modifier
                        .align(Alignment.TopEnd)
                        .size(30.dp)
                        .clickable { shouldShowDialog.value=false }
                )
            }
        }
    }
}
tf7tbtn2

tf7tbtn21#

您需要将shouldShowDialog的状态重置为true
我建议您创建一个lambda to GetBifImage组合如下:

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun GetBifImage(photoUrl: String, onDismiss: () -> Unit) {
    Dialog(
        properties = DialogProperties(usePlatformDefaultWidth = false),
        onDismissRequest = { onDismiss.invoke() }
    ) {
        Box(modifier = Modifier.fillMaxSize()) {
            ZoomImage(photoUrl)
            Image(
                painter = painterResource(id = R.drawable.ic_close_),
                contentDescription = "",
                modifier = Modifier
                    .align(Alignment.TopEnd)
                    .size(30.dp)
                    .clickable { onDismiss.invoke() }
            )
        }
    }
}

因此,您需要修改对AnimalImagies的调用

@Composable
fun AnimalImagies(photoUrl: String) {
   var dialogOpen by remember { mutableStateOf(false) }

    val imageLoader = LocalContext.current.imageLoader.newBuilder()
        .logger(DebugLogger())
        .build()
    AsyncImage(
        model = ImageRequest.Builder(LocalContext.current)
            .data(photoUrl)
            .crossfade(true)
            .build(),
        imageLoader = imageLoader,
        contentDescription = "",
        contentScale = ContentScale.Crop,
        modifier = Modifier
            .size(140.dp)
            .clip(RoundedCornerShape(corner = CornerSize(16.dp)))
            .clickable(onClick = {
                dialogOpen = true
            })
    )

    if (dialogOpen) {
        GetBifImage(photoUrl = photoUrl) {
            dialogOpen = false
        }
    }
}

现在当你点击Image,lambda函数会改变dialogOpen

相关问题