Skip to content

Instantly share code, notes, and snippets.

@gsteckman

gsteckman/App.kt Secret

Created November 15, 2020 22:20
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 gsteckman/bbfb046673504935388a6eafc9eb652b to your computer and use it in GitHub Desktop.
Save gsteckman/bbfb046673504935388a6eafc9eb652b to your computer and use it in GitHub Desktop.
A not correctly working implementation of a master-detail user interface with compose desktop 0.1.0-m1-build62
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.width
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
data class Item(val name: String, val value: String)
@Composable
fun App() {
val items = remember {
mutableStateListOf(Item("First", "one"), Item("Second", "two"), Item("Third", "three"))
}
var selectedItemIndex by remember { mutableStateOf(0) }
MaterialTheme {
Row {
ItemsList(items, selectedItemIndex, modifier = Modifier.width(150.dp)) {
selectedItemIndex = it
}
ItemForm(items[selectedItemIndex]) {
items[selectedItemIndex] = it
}
}
}
}
@Composable
fun ItemForm(item: Item, saveAction: (Item) -> Unit) {
var tempItem by remember(item) { mutableStateOf(item) }
Column {
TextField(
value = tempItem.name,
label = { Text("Name") },
onValueChange = {
tempItem = tempItem.copy(name = it)
}
)
TextField(
value = tempItem.value,
label = { Text("Value") },
onValueChange = {
tempItem = tempItem.copy(value = it)
}
)
Button(onClick = { saveAction(tempItem) }) {
Text("Save")
}
}
}
@Composable
fun ItemsList(listItems: List<Item>, selectedIndex: Int, modifier: Modifier = Modifier, selectAction: (Int) -> Unit) {
Column(modifier) {
listItems.forEachIndexed { index, item ->
val m = if (selectedIndex == index) {
Modifier.background(MaterialTheme.colors.secondary)
} else {
Modifier.background(MaterialTheme.colors.background)
}
ListItem(modifier = m.clickable { selectAction(index) }) {
Text(item.name)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment