|
16 | 16 |
|
17 | 17 | package com.example.compose.snippets.components
|
18 | 18 |
|
| 19 | +import androidx.compose.foundation.layout.Arrangement |
19 | 20 | import androidx.compose.foundation.layout.Box
|
20 | 21 | import androidx.compose.foundation.layout.Column
|
| 22 | +import androidx.compose.foundation.layout.Row |
21 | 23 | import androidx.compose.foundation.layout.fillMaxSize
|
22 | 24 | import androidx.compose.foundation.layout.fillMaxWidth
|
23 | 25 | import androidx.compose.foundation.layout.padding
|
| 26 | +import androidx.compose.foundation.layout.wrapContentSize |
24 | 27 | import androidx.compose.material.icons.Icons
|
| 28 | +import androidx.compose.material.icons.automirrored.filled.DirectionsBike |
| 29 | +import androidx.compose.material.icons.automirrored.filled.DirectionsRun |
| 30 | +import androidx.compose.material.icons.automirrored.filled.DirectionsWalk |
25 | 31 | import androidx.compose.material.icons.automirrored.outlined.Help
|
26 | 32 | import androidx.compose.material.icons.automirrored.outlined.OpenInNew
|
27 | 33 | import androidx.compose.material.icons.automirrored.outlined.Send
|
| 34 | +import androidx.compose.material.icons.filled.ArrowDropDown |
| 35 | +import androidx.compose.material.icons.filled.Check |
| 36 | +import androidx.compose.material.icons.filled.Hiking |
28 | 37 | import androidx.compose.material.icons.filled.MoreVert
|
| 38 | +import androidx.compose.material.icons.filled.Tune |
29 | 39 | import androidx.compose.material.icons.outlined.Feedback
|
30 | 40 | import androidx.compose.material.icons.outlined.Info
|
31 | 41 | import androidx.compose.material.icons.outlined.Person
|
32 | 42 | import androidx.compose.material.icons.outlined.Settings
|
33 | 43 | import androidx.compose.material3.Button
|
34 | 44 | import androidx.compose.material3.DropdownMenu
|
35 | 45 | import androidx.compose.material3.DropdownMenuItem
|
| 46 | +import androidx.compose.material3.FilterChip |
36 | 47 | import androidx.compose.material3.FloatingActionButton
|
37 | 48 | import androidx.compose.material3.HorizontalDivider
|
38 | 49 | import androidx.compose.material3.Icon
|
@@ -76,6 +87,9 @@ fun MenusExamples() {
|
76 | 87 | Button(onClick = { currentExample = { DropdownMenuWithDetails() } }) {
|
77 | 88 | Text("Dropdown menu with sections and icons")
|
78 | 89 | }
|
| 90 | + Button(onClick = { currentExample = { DropdownFilter() } }) { |
| 91 | + Text("Menu for applying a filter, attached to a filter chip") |
| 92 | + } |
79 | 93 | }
|
80 | 94 | }
|
81 | 95 | }
|
@@ -212,3 +226,79 @@ fun DropdownMenuWithDetails() {
|
212 | 226 | fun DropdownMenuWithDetailsPreview() {
|
213 | 227 | DropdownMenuWithDetails()
|
214 | 228 | }
|
| 229 | + |
| 230 | +@Composable |
| 231 | +fun DropdownFilter(modifier: Modifier = Modifier) { |
| 232 | + Row( |
| 233 | + modifier = modifier |
| 234 | + .padding(16.dp) |
| 235 | + .wrapContentSize(unbounded = true), |
| 236 | + horizontalArrangement = Arrangement.spacedBy(8.dp), |
| 237 | + verticalAlignment = Alignment.CenterVertically |
| 238 | + ) { |
| 239 | + Icon(Icons.Default.Tune, "Filters") |
| 240 | + FilterChip(selected = false, onClick = { /*TODO*/ }, label = { Text("Time") }) |
| 241 | + DropdownFilterChip() |
| 242 | + FilterChip(selected = false, onClick = { /*TODO*/ }, label = { Text("Wheelchair accessible") }) |
| 243 | + } |
| 244 | +} |
| 245 | + |
| 246 | +// [START android_compose_components_dropdownfilterchip] |
| 247 | +@Composable |
| 248 | +fun DropdownFilterChip(modifier: Modifier = Modifier) { |
| 249 | + var isDropdownExpanded by remember { mutableStateOf(false) } |
| 250 | + var selectedChipText by remember { mutableStateOf<String?>(null) } |
| 251 | + Box(modifier) { |
| 252 | + FilterChip( |
| 253 | + selected = selectedChipText != null, |
| 254 | + onClick = { isDropdownExpanded = !isDropdownExpanded }, |
| 255 | + label = { Text(if (selectedChipText == null) "Type" else "$selectedChipText") }, |
| 256 | + leadingIcon = { if (selectedChipText != null) Icon(Icons.Default.Check, null) }, |
| 257 | + trailingIcon = { Icon(Icons.Default.ArrowDropDown, null) }, |
| 258 | + ) |
| 259 | + DropdownMenu( |
| 260 | + expanded = isDropdownExpanded, |
| 261 | + onDismissRequest = { isDropdownExpanded = !isDropdownExpanded } |
| 262 | + ) { |
| 263 | + DropdownMenuItem( |
| 264 | + text = { Text("Running") }, |
| 265 | + leadingIcon = { Icon(Icons.AutoMirrored.Default.DirectionsRun, null) }, |
| 266 | + onClick = { |
| 267 | + selectedChipText = |
| 268 | + if (selectedChipText == "Running") null else "Running" |
| 269 | + } |
| 270 | + ) |
| 271 | + DropdownMenuItem( |
| 272 | + text = { Text("Walking") }, |
| 273 | + leadingIcon = { Icon(Icons.AutoMirrored.Default.DirectionsWalk, null) }, |
| 274 | + onClick = { |
| 275 | + selectedChipText = |
| 276 | + if (selectedChipText == "Walking") null else "Walking" |
| 277 | + } |
| 278 | + ) |
| 279 | + DropdownMenuItem( |
| 280 | + text = { Text("Hiking") }, |
| 281 | + leadingIcon = { Icon(Icons.Default.Hiking, null) }, |
| 282 | + onClick = { |
| 283 | + selectedChipText = |
| 284 | + if (selectedChipText == "Hiking") null else "Hiking" |
| 285 | + } |
| 286 | + ) |
| 287 | + DropdownMenuItem( |
| 288 | + text = { Text("Cycling") }, |
| 289 | + leadingIcon = { Icon(Icons.AutoMirrored.Default.DirectionsBike, null) }, |
| 290 | + onClick = { |
| 291 | + selectedChipText = |
| 292 | + if (selectedChipText == "Cycling") null else "Cycling" |
| 293 | + } |
| 294 | + ) |
| 295 | + } |
| 296 | + } |
| 297 | +} |
| 298 | +// [END android_compose_components_dropdownfilterchip] |
| 299 | + |
| 300 | +@Preview |
| 301 | +@Composable |
| 302 | +private fun DropdownFilterPreview() { |
| 303 | + DropdownFilter() |
| 304 | +} |
0 commit comments