Skip to content

Instantly share code, notes, and snippets.

@SanjayDevTech
Last active January 20, 2024 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SanjayDevTech/b8028ed3df571d78921311b22899de34 to your computer and use it in GitHub Desktop.
Save SanjayDevTech/b8028ed3df571d78921311b22899de34 to your computer and use it in GitHub Desktop.
// import ....
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
// UI State
data class UIState(val name: String, val title: String)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
EditUI() // <-- Called EditUI
}
}
}
}
}
@Composable
fun EditUI() {
var uiState by remember {
mutableStateOf(UIState("", ""))
}
Column(
modifier = Modifier.padding(horizontal = 16.dp)
) {
TextField(
modifier = Modifier.fillMaxWidth(),
value = uiState.name, onValueChange = {
uiState = uiState.copy(name = it) // <-- Trigger recomposition
}, placeholder = {
Text(text = "Name")
})
TextField(
modifier = Modifier.fillMaxWidth(),
value = uiState.title, onValueChange = {
uiState = uiState.copy(title = it) // <-- Trigger recomposition
}, placeholder = {
Text(text = "Title")
})
DisplayUI(uiState)
}
}
@Composable
fun DisplayUI(uiState: UIState) {
Column {
Text(text = uiState.name)
Text(text = uiState.title)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment