Skip to content

Instantly share code, notes, and snippets.

@euri16
Created May 28, 2025 14:36
Show Gist options
  • Save euri16/b42034b89b786452b35d62dc658781e7 to your computer and use it in GitHub Desktop.
Save euri16/b42034b89b786452b35d62dc658781e7 to your computer and use it in GitHub Desktop.
@Composable
fun UserScreen(viewModel: UserViewModel = viewModel()) {
// Collect state from the ViewModel
val username by viewModel.username.collectAsStateWithLifecycle()
val age by viewModel.age.collectAsStateWithLifecycle()
Column(modifier = Modifier.padding(16.dp)) {
TextField(
value = username,
onValueChange = { viewModel.onUsernameChanged(it) },
label = { Text("Username") }
)
Text(text = "Age: $age")
Button(onClick = { viewModel.onAgeIncrement() }, modifier = Modifier.padding(top = 8.dp)) {
Text("Increase Age")
}
Button(onClick = { viewModel.onAgeDecrement() }, modifier = Modifier.padding(top = 8.dp)) {
Text("Decrease Age")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment