隐藏/显示 CircularProgressIndicator onclick Jetpack Compose

时间:2021-07-27 15:27:48

标签: android-jetpack-compose

我想在点击时显示 CircularProgressIndicator 然后隐藏 !the CircularProgressIndicator 在函数执行时看到下面的代码

var loader by remember {mutableStateOf(false)}
if(loader){
   CircularProgressIndicator()
}
Button (onclick {
   loader = !loader // show CircularProgressIndicator
   // Function which is to be executed 
   // After function is executed which usually takes 2 sec then after that CircularProgressIndicator should be hidden
   loader = loader // Not hidding
}){
  Text("Toggle")
}

1 个答案:

答案 0 :(得分:0)

基本上,您需要检查在处理结束时在任何情况下都应该调用 firebase = false。我建议你至少将逻辑移到一个单独的函数中,这样我就更难犯错了,但基本上它应该是这样的:

OutlinedButton(onClick = {
    keyboardController?.hide()
    if (emailState.value.isEmpty() && secureState.value.isEmpty())
        return@OutlinedButton
    firebase = true
    FirebaseAuth.getInstance()
        .createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener { task ->
            val exception = task.exception?.localizedMessage.toString()
            if (task.isSuccessful)
                FirebaseAuth.getInstance().currentUser?.sendEmailVerification()
                    ?.addOnCompleteListener { task ->
                        firebase = true
                        if (task.isSuccessful)
                            snackbarCoroutineScope.launch {
                                scaffoldState.snackbarHostState.showSnackbar(
                                    "Check your Email for Verification")
                            }
                    }
            else
                snackbarCoroutineScope.launch {
                    scaffoldState.snackbarHostState.showSnackbar(exception)
                    firebase = true
                }
        }
}) {
    Text(text = "Sign Up")
}
Spacer(modifier = Modifier.size(16.dp))
TextButton(onClick = {
    keyboardController?.hide()
    if (emailState.value.isEmpty())
        return@TextButton
    FirebaseAuth.getInstance().sendPasswordResetEmail(email)
        .addOnCompleteListener { task ->
            val exception = task.exception?.localizedMessage.toString()
            firebase = false
            if (task.isSuccessful)
                snackbarCoroutineScope.launch {
                    scaffoldState.snackbarHostState.showSnackbar("Check your Email for Password reset")
                    firebase = true
                }
            else
                snackbarCoroutineScope.launch {
                    scaffoldState.snackbarHostState.showSnackbar(exception)
                    firebase = true
                }
        }
}) {
    Text(text = "Forgot password?")
}