mutableStateOf 在视图模型中更新但在@Compose 中没有效果

mutableStateOf updating in view model but no effect in @Compose

我的 HiltViewModel 中确实有一个名为 loading 的 mutableStateof。我正在更新从视图模型中的函数加载的值,该函数正在传递给@composable 函数并在那里使用。 情况是加载值在 viewModel 中完美更新,但未反映在 @Composable 函数中。 我的视图模型是:

@HiltViewModel
class AuthenticationViewModel @Inject constructor(
    private val repository: AppRepository,
    application: Application
): ViewModel() {
   val loading = mutableStateOf(false)

   fun update() = viewModelScope.launch {
      loading.value = true
   }
}

加载值在此处更新但未反映在@composable

@Composable
fun LoginScreen(
    viewModel: AuthenticationViewModel,
    actions: MainActions
) {
    val loading = viewModel.loading.value

    //Loading is another composable function where bool value is passed
    Loading(state = loading)
    CustomNavigationButton(title = "Sign In",enabled = true,onClick = 
    {viewModel.update()})
}

现在,当我点击导航按钮时,视图模型函数被调用,加载状态也在更新,但没有反映回@Composable

加载可组合项是:

@Composable
fun Loading(state:Boolean) {
    var showDialog by remember { mutableStateOf(state) }

    if(showDialog){
        Dialog(onDismissRequest = { showDialog = false }, DialogProperties(
            dismissOnBackPress = false,dismissOnClickOutside = false
        )) {
            Box(
                modifier = Modifier.size(100.dp).background(Color.White, shape = RoundedCornerShape(8.dp)),
                contentAlignment= Alignment.Center,
            ){
                CircularProgressIndicator()
            }
        }
    }
}

问题出在 Loader 可组合函数中。 if condition 始终设置为 false,因为可变状态从未在该特定可组合函数中更新。 已替换:

@Composable
fun Loading(state:Boolean) {
    var showDialog by remember { mutableStateOf(state) }

    if(showDialog){
        Dialog(onDismissRequest = { showDialog = false }, DialogProperties(
            dismissOnBackPress = false,dismissOnClickOutside = false
        )) {
            Box(
                modifier = Modifier.size(100.dp).background(Color.White, shape = RoundedCornerShape(8.dp)),
                contentAlignment= Alignment.Center,
            ){
                CircularProgressIndicator()
            }
        }
    }
}

@Composable
fun Loading(state:Boolean) {
    if(state){
        Dialog(onDismissRequest = {}, DialogProperties(
            dismissOnBackPress = false,dismissOnClickOutside = false
        )) {
            Box(
                modifier = Modifier.size(100.dp).background(Color.White, shape = RoundedCornerShape(8.dp)),
                contentAlignment= Alignment.Center,
            ){
                CircularProgressIndicator()
            }
        }
    }
}