-
-
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
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 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