Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Demo examples #1126

Merged
merged 2 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.items
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import kotlin.random.Random
Expand Down Expand Up @@ -82,15 +84,21 @@ private fun ExampleLazyGrid() {
}
}

@OptIn(ExperimentalFoundationApi::class)
private data class StaggeredGridItem(val color: Color, val height: Dp)

@Composable
private fun ExampleStaggeredGrid() {
val items: List<StaggeredGridItem> = remember {
List(100) {
StaggeredGridItem(color = Color(Random.nextInt()), height = Random.nextInt(100, 200).dp)
}
}
LazyVerticalStaggeredGrid(StaggeredGridCells.Fixed(3), Modifier.fillMaxSize()) {
items(100) {
items(items) {
Box(
Modifier.fillMaxSize()
.height(remember { Random.nextInt(100, 200).dp })
.background(remember { Color(Random.nextInt()) })
.height(it.height)
.background(it.color)
)
}
}
Expand Down Expand Up @@ -125,9 +133,9 @@ private fun ExampleTwoDirectionsAndRTL() {
Text("Toggle layout direction")
}
LazyColumn(Modifier.fillMaxSize()) {
items(rows) {row ->
items(rows) { row ->
LazyRow(Modifier.height(rowHeight)) {
items(columns) {col ->
items(columns) { col ->
val color = colors[(row + col) % colors.size]

Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
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.graphics.Color
import androidx.compose.ui.unit.dp
Expand All @@ -20,26 +21,27 @@ fun EmojiExample() {
Column {
TextFieldExample(
"Compound Emoji",
mutableStateOf("Family emoji: \uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66, and some text at the end")
"Family emoji: \uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66, and some text at the end"
)

TextFieldExample(
"Compound Emoji",
mutableStateOf("Split family emoji: \uD83D\uDC68 \uD83D\uDC69 \uD83D\uDC67 \uD83D\uDC66, and some text at the end")
"Split family emoji: \uD83D\uDC68 \uD83D\uDC69 \uD83D\uDC67 \uD83D\uDC66, and some text at the end"
)

}

}

@Composable
private fun TextFieldExample(title: String, state: MutableState<String>) {
private fun TextFieldExample(title: String, initialText: String) {
var text by remember { mutableStateOf(initialText) }
Column(Modifier.fillMaxWidth().padding(4.dp).border(1.dp, Color.Black).padding(4.dp)) {
Text(title)
TextField(
value = state.value,
value = text,
onValueChange = {
state.value = it
text = it
},
keyboardOptions = KeyboardOptions(autoCorrect = false),
)
Expand Down
Loading