-
-
Save SanjayDevTech/b8028ed3df571d78921311b22899de34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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