-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDropdown.kt
More file actions
58 lines (53 loc) · 1.69 KB
/
Copy pathDropdown.kt
File metadata and controls
58 lines (53 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package dev.snipme.androidexample
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
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.draw.clip
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
@Composable
fun Dropdown(
options: List<String>,
selected: Int,
modifier: Modifier = Modifier,
onSelect: (String) -> Unit,
) {
var selectedOption by remember {
mutableStateOf(options[selected])
}
var isExpanded by remember { mutableStateOf(false) }
DropdownMenu(
expanded = isExpanded,
onDismissRequest = { isExpanded = false },
) {
options.forEach { option ->
DropdownMenuItem(
text = { Text(text = option) },
onClick = {
selectedOption = option
onSelect(option)
isExpanded = !isExpanded
},
)
}
}
Text(
text = selectedOption,
textAlign = TextAlign.Center,
modifier = modifier
.clip(RoundedCornerShape(16.dp))
.fillMaxWidth()
.clickable { isExpanded = !isExpanded }
.padding(8.dp),
)
}