5.13

当在主屏幕创建工厂时

fun LoginScreen(
    viewModel: LoginViewModel = viewModel(factory = LoginViewModelProvider.Factory)
)

下面这段代码要封装在一个函数中 并以另一个函数 作为一个中介
置于为什么 还不知道 是慢慢 试出来的

fun LoginContent(
    form: LoginAndRegistForm,
    onLoginUsernameChange: (String) -> Unit,
    onLoginPasswordChange: (String) -> Unit
) {
    Column(
        modifier = Modifier.padding(8.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        OutlinedTextField(
            enabled = true,
            modifier = Modifier,
            label = { Text(text = stringResource(id = R.string.username)) },
            value = form.loginUsername,
            //输入的值发生变化时
            onValueChange = { onLoginUsernameChange(it) }
        )
        OutlinedTextField(
            enabled = true,
            modifier = Modifier,
            label = { Text(text = stringResource(id = R.string.password)) },
            value = form.loginPassword,
            //输入的值发生变化时
            onValueChange = { onLoginPasswordChange(it) }
        )
    }
}

用下面的函数作为中介

@Composable
fun LoginBody(
    viewModel: LoginViewModel
) {
    LoginContent(
        form = viewModel.form.value,
        onLoginUsernameChange = viewModel::onLoginUsernameChange,
        onLoginPasswordChange = viewModel::onLoginPasswordChange
    )

}

放到主屏幕

 Column(modifier = Modifier.padding(innerPadding)) {
                LoginBody(viewModel)
                Row {
                    Button(onClick = { }) {
                        Text(text = stringResource(id = R.string.login))
                    }
                    Spacer(modifier = Modifier.padding(horizontal = 15.dp))
                    Button(onClick = { expanded = false }) {
                        Text(text = stringResource(id = R.string.regist))
                    }
                }
            }
posted @ 2024-05-13 23:27  258333  阅读(6)  评论(0编辑  收藏  举报