-
-
Save Kaaveh/f7814bf3ecbb4ada2cea617680dcb24a 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
@Composable | |
fun ContactList( | |
contacts: List<Contact>, | |
comparator: Comparator<Contact>, | |
modifier: Modifier = Modifier | |
) { | |
LazyColumn(modifier) { | |
// DON’T DO THIS | |
items(contacts.sortedWith(comparator)) { contact -> | |
// ... | |
} | |
} | |
} |
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
// Here, assume animateColorBetween() is a function that swaps between | |
// two colors | |
val color by animateColorBetween(Color.Cyan, Color.Magenta) | |
Box(Modifier.fillMaxSize().background(color)) |
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
val color by animateColorBetween(Color.Cyan, Color.Magenta) | |
Box( | |
Modifier | |
.fillMaxSize() | |
.drawBehind { | |
drawRect(color) | |
} | |
) |
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
@Composable | |
fun BadComposable() { | |
var count by remember { mutableStateOf(0) } | |
// Causes recomposition on click | |
Button( = { count++ }, modifier = Modifier.wrapContentSize()) { | |
Text("Recompose") | |
} | |
Text("$count") | |
count++ // Backwards write, writing to state after it has been read | |
} |
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
@Composable | |
fun ContactList( | |
contacts: List<Contact>, | |
comparator: Comparator<Contact>, | |
modifier: Modifier = Modifier | |
) { | |
val sortedContacts = remember(contacts, sortComparator) { | |
contacts.sortedWith(sortComparator) | |
} | |
LazyColumn(modifier) { | |
items(sortedContacts) { | |
// ... | |
} | |
} | |
} |
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
@Composable | |
fun NotesList(notes: List<Note>) { | |
LazyColumn { | |
items( | |
items = notes | |
) { note -> | |
NoteRow(note) | |
} | |
} | |
} |
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
@Composable | |
fun NotesList(notes: List<Note>) { | |
LazyColumn { | |
items( | |
items = notes, | |
key = { note -> | |
// Return a stable, unique key for the note | |
note.id | |
} | |
) { note -> | |
NoteRow(note) | |
} | |
} | |
} |
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
val listState = rememberLazyListState() | |
LazyColumn(state = listState) { | |
// ... | |
} | |
val showButton = listState.firstVisibleItemIndex > 0 | |
AnimatedVisibility(visible = showButton) { | |
ScrollToTopButton() | |
} |
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
val listState = rememberLazyListState() | |
LazyColumn(state = listState) { | |
// ... | |
} | |
val showButton by remember { | |
derivedStateOf { | |
listState.firstVisibleItemIndex > 0 | |
} | |
} | |
AnimatedVisibility(visible = showButton) { | |
ScrollToTopButton() | |
} |
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
@Composable | |
fun SnackDetail() { | |
// ... | |
Box(Modifier.fillMaxSize()) { // Recomposition Scope Start | |
val scroll = rememberScrollState(0) | |
// ... | |
Title(snack, scroll.value) | |
// ... | |
} // Recomposition Scope End | |
} | |
@Composable | |
private fun Title(snack: Snack, scroll: Int) { | |
// ... | |
val offset = with(LocalDensity.current) { scroll.toDp() } | |
Column( | |
modifier = Modifier | |
.offset(y = offset) | |
) { | |
// ... | |
} | |
} |
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
@Composable | |
fun SnackDetail() { | |
// ... | |
Box(Modifier.fillMaxSize()) { // Recomposition Scope Start | |
val scroll = rememberScrollState(0) | |
// ... | |
Title(snack) { scroll.value } | |
// ... | |
} // Recomposition Scope End | |
} | |
@Composable | |
private fun Title(snack: Snack, scrollProvider: () -> Int) { | |
// ... | |
val offset = with(LocalDensity.current) { scrollProvider().toDp() } | |
Column( | |
modifier = Modifier | |
.offset(y = offset) | |
) { | |
// ... | |
} | |
} |
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
@Composable | |
private fun Title(snack: Snack, scrollProvider: () -> Int) { | |
// ... | |
Column( | |
modifier = Modifier | |
.offset { IntOffset(x = 0, y = scrollProvider()) } | |
) { | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment