android 滑块移动jetpack组合时无法按下按钮

jc3wubiy  于 2023-02-17  发布在  Android
关注(0)|答案(1)|浏览(119)

我有一个简单的滑块,我需要禁用按钮在星星和当用户移动滑块,它应该是启用,我可以禁用它在第一次,但我不能启用它回来,由于某种原因,它没有采取新的变量值这是我的代码.我的想法是因为变量是在第一次声明和滑块值是封装不知何故?但如果是这种情况下,我如何将值传递给组件?

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun PopupWindowDialogStudent(
    onConfirm: (Int, String) -> Unit,
    parentUiState: StudentHomeUiState,
) {
    // val openDialog = remember { mutableStateOf(parentUiState.showInAppFeedback) }
    val openDialog = remember { mutableStateOf(!parentUiState.showInAppFeedback) }
    var sliderPosition by remember { mutableStateOf(5f) }
    var enable by remember { mutableStateOf(false) }

    val recommend = sliderPosition.toInt()
    Column(
    ) {
        Box {
            if (openDialog.value) {
                Dialog(
                    onDismissRequest = { openDialog.value = false },
                    properties = DialogProperties(),
                )
                {
                    Box(
                        Modifier
                            .fillMaxWidth()
                            .height(500.dp)
                            .background(Color.White, RoundedCornerShape(10.dp))
                            .border(1.dp, color = Color.White, RoundedCornerShape(20.dp))
                    ) {
                        Column(
                            modifier = Modifier
                                .fillMaxSize()
                                .fillMaxHeight()
                                .padding(horizontal = 5.dp)
                                .verticalScroll(rememberScrollState())
                            ,
                            horizontalAlignment = Alignment.CenterHorizontally,
                            verticalArrangement = Arrangement.Center
                        ) {
                            Column(horizontalAlignment = Alignment.CenterHorizontally,
                                verticalArrangement = Arrangement.Center) {
                                Row(
                                    verticalAlignment = Alignment.CenterVertically,
                                    horizontalArrangement = Arrangement.Center,
                                    modifier = Modifier
                                        .fillMaxWidth()
                                        .padding(vertical = 10.dp)) {
                                    var completeValue by remember { mutableStateOf("") }
                                    completeValue = sliderPosition.toString()

                                    Column(horizontalAlignment = Alignment.CenterHorizontally){
                                        Slider(
                                            value = sliderPosition,
                                            onValueChange = {
                                                sliderPosition = it
                                                var enable = true
                                            },
                                            valueRange = 0f..10f,
                                            steps = 9,
                                            onValueChangeFinished = {
                                                completeValue
                                            },
                                            modifier = Modifier.width(180.dp),
                                            colors = SliderDefaults.colors(
                                                thumbColor = secondaryColor,
                                                activeTrackColor = Color.Blue
                                            )

                                        )
                                    }
                                }
                                
                                Spacer(modifier = Modifier.height(10.dp))
                                var comment by remember { mutableStateOf("") }
                                val keyboardController = LocalSoftwareKeyboardController.current

                                TextField(
                                    value = comment,
                                    placeholder = { Text(text = "¿Tienes algún otro comentario?") },
                                    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
                                    keyboardActions = KeyboardActions(
                                        onDone = {keyboardController?.hide()}),
                                    onValueChange = {
                                        comment = it
                                    }
                                )
                                Spacer(modifier = Modifier.height(10.dp))
                                if (enable){
                                    Button(
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .padding(10.dp),
                                        onClick = {
                                            onConfirm(recommend,comment)
                                            openDialog.value = !openDialog.value
                                        }
                                    ) {
                                        Text(
                                            text = "¡Contesta y gana +20 puntos!",
                                            style = MaterialTheme.typography.subtitle2,
                                            fontWeight = FontWeight.Bold,
                                            modifier = Modifier.padding(3.dp))
                                    }
                                }else{
                                    Button(
                                        modifier = Modifier
                                            .fillMaxWidth()
                                            .padding(10.dp),
                                        onClick = {
                                        }
                                    ) {
                                        Text(
                                            text = "¡Contesta y gana +20 puntos!",
                                            style = MaterialTheme.typography.subtitle2,
                                            fontWeight = FontWeight.Bold,
                                            modifier = Modifier.padding(3.dp))
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }
    }
}
xqk2d5yq

xqk2d5yq1#

Slider中,您可以使用onValueChangeFinished更改enable值:

var enable by remember { mutableStateOf(false) }                                 

   Slider(
        value = sliderPosition,
        onValueChange = {
            sliderPosition = it
            enable = true
        },
        onValueChangeFinished = {
            completeValue
            enable = false
        },
        //...
    )

另外,您可以避免使用if语句来处理Button,只需用途:

Button(
        //...
        enabled = enable,
        onClick = {
            openDialog.value = !openDialog.value
        }
    ) {
        //...
    }

相关问题