Repository navigation
Expand file tree
/
Copy pathMenus.kt
More file actions
304 lines (284 loc) · 10.9 KB
/
Copy pathMenus.kt
File metadata and controls
304 lines (284 loc) · 10.9 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.compose.snippets.components
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.DirectionsBike
import androidx.compose.material.icons.automirrored.filled.DirectionsRun
import androidx.compose.material.icons.automirrored.filled.DirectionsWalk
import androidx.compose.material.icons.automirrored.outlined.Help
import androidx.compose.material.icons.automirrored.outlined.OpenInNew
import androidx.compose.material.icons.automirrored.outlined.Send
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Hiking
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Tune
import androidx.compose.material.icons.outlined.Feedback
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material.icons.outlined.Person
import androidx.compose.material.icons.outlined.Settings
import androidx.compose.material3.Button
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
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.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
@Composable
fun MenusExamples() {
var currentExample by remember { mutableStateOf<(@Composable () -> Unit)?>(null) }
Box(modifier = Modifier.fillMaxSize()) {
currentExample?.let {
it()
FloatingActionButton(
onClick = { currentExample = null },
modifier = Modifier
.align(Alignment.BottomEnd)
.padding(16.dp)
) {
Text(text = "Close example", modifier = Modifier.padding(16.dp))
}
return
}
Column(modifier = Modifier.padding(16.dp)) {
Button(onClick = { currentExample = { MinimalDropdownMenu() } }) {
Text("Minimal dropdown menu")
}
Button(onClick = { currentExample = { LongBasicDropdownMenu() } }) {
Text("Dropdown menu with many items")
}
Button(onClick = { currentExample = { DropdownMenuWithDetails() } }) {
Text("Dropdown menu with sections and icons")
}
Button(onClick = { currentExample = { DropdownFilter() } }) {
Text("Menu for applying a filter, attached to a filter chip")
}
}
}
}
// [START android_compose_components_minimaldropdownmenu]
@Composable
fun MinimalDropdownMenu() {
var expanded by remember { mutableStateOf(false) }
Box(
modifier = Modifier
.padding(16.dp)
) {
IconButton(onClick = { expanded = !expanded }) {
Icon(Icons.Default.MoreVert, contentDescription = "More options")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
DropdownMenuItem(
text = { Text("Option 1") },
onClick = { /* Do something... */ }
)
DropdownMenuItem(
text = { Text("Option 2") },
onClick = { /* Do something... */ }
)
}
}
}
// [END android_compose_components_minimaldropdownmenu]
@Preview
@Composable
fun MinimalDropdownMenuPreview() {
MinimalDropdownMenu()
}
// [START android_compose_components_longbasicdropdownmenu]
@Composable
fun LongBasicDropdownMenu() {
var expanded by remember { mutableStateOf(false) }
// Placeholder list of 100 strings for demonstration
val menuItemData = List(100) { "Option ${it + 1}" }
Box(
modifier = Modifier
.padding(16.dp)
) {
IconButton(onClick = { expanded = !expanded }) {
Icon(Icons.Default.MoreVert, contentDescription = "More options")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
menuItemData.forEach { option ->
DropdownMenuItem(
text = { Text(option) },
onClick = { /* Do something... */ }
)
}
}
}
}
// [END android_compose_components_longbasicdropdownmenu]
@Preview
@Composable
fun LongBasicDropdownMenuPreview() {
LongBasicDropdownMenu()
}
// [START android_compose_components_dropdownmenuwithdetails]
@Composable
fun DropdownMenuWithDetails() {
var expanded by remember { mutableStateOf(false) }
Box(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
IconButton(onClick = { expanded = !expanded }) {
Icon(Icons.Default.MoreVert, contentDescription = "More options")
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false }
) {
// First section
DropdownMenuItem(
text = { Text("Profile") },
leadingIcon = { Icon(Icons.Outlined.Person, contentDescription = null) },
onClick = { /* Do something... */ }
)
DropdownMenuItem(
text = { Text("Settings") },
leadingIcon = { Icon(Icons.Outlined.Settings, contentDescription = null) },
onClick = { /* Do something... */ }
)
HorizontalDivider()
// Second section
DropdownMenuItem(
text = { Text("Send Feedback") },
leadingIcon = { Icon(Icons.Outlined.Feedback, contentDescription = null) },
trailingIcon = { Icon(Icons.AutoMirrored.Outlined.Send, contentDescription = null) },
onClick = { /* Do something... */ }
)
HorizontalDivider()
// Third section
DropdownMenuItem(
text = { Text("About") },
leadingIcon = { Icon(Icons.Outlined.Info, contentDescription = null) },
onClick = { /* Do something... */ }
)
DropdownMenuItem(
text = { Text("Help") },
leadingIcon = { Icon(Icons.AutoMirrored.Outlined.Help, contentDescription = null) },
trailingIcon = { Icon(Icons.AutoMirrored.Outlined.OpenInNew, contentDescription = null) },
onClick = { /* Do something... */ }
)
}
}
}
// [END android_compose_components_dropdownmenuwithdetails]
@Preview
@Composable
fun DropdownMenuWithDetailsPreview() {
DropdownMenuWithDetails()
}
@Composable
fun DropdownFilter(modifier: Modifier = Modifier) {
Row(
modifier = modifier
.padding(16.dp)
.wrapContentSize(unbounded = true),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalAlignment = Alignment.CenterVertically
) {
Icon(Icons.Default.Tune, "Filters")
FilterChip(selected = false, onClick = { /*TODO*/ }, label = { Text("Time") })
DropdownFilterChip()
FilterChip(selected = false, onClick = { /*TODO*/ }, label = { Text("Wheelchair accessible") })
}
}
// [START android_compose_components_dropdownfilterchip]
@Composable
fun DropdownFilterChip(modifier: Modifier = Modifier) {
var isDropdownExpanded by remember { mutableStateOf(false) }
var selectedChipText by remember { mutableStateOf<String?>(null) }
Box(modifier) {
FilterChip(
selected = selectedChipText != null,
onClick = { isDropdownExpanded = !isDropdownExpanded },
label = { Text(if (selectedChipText == null) "Type" else "$selectedChipText") },
leadingIcon = { if (selectedChipText != null) Icon(Icons.Default.Check, null) },
trailingIcon = { Icon(Icons.Default.ArrowDropDown, null) },
)
DropdownMenu(
expanded = isDropdownExpanded,
onDismissRequest = { isDropdownExpanded = !isDropdownExpanded }
) {
DropdownMenuItem(
text = { Text("Running") },
leadingIcon = { Icon(Icons.AutoMirrored.Default.DirectionsRun, null) },
onClick = {
selectedChipText =
if (selectedChipText == "Running") null else "Running"
}
)
DropdownMenuItem(
text = { Text("Walking") },
leadingIcon = { Icon(Icons.AutoMirrored.Default.DirectionsWalk, null) },
onClick = {
selectedChipText =
if (selectedChipText == "Walking") null else "Walking"
}
)
DropdownMenuItem(
text = { Text("Hiking") },
leadingIcon = { Icon(Icons.Default.Hiking, null) },
onClick = {
selectedChipText =
if (selectedChipText == "Hiking") null else "Hiking"
}
)
DropdownMenuItem(
text = { Text("Cycling") },
leadingIcon = { Icon(Icons.AutoMirrored.Default.DirectionsBike, null) },
onClick = {
selectedChipText =
if (selectedChipText == "Cycling") null else "Cycling"
}
)
}
}
}
// [END android_compose_components_dropdownfilterchip]
@Preview
@Composable
private fun DropdownFilterPreview() {
DropdownFilter()
}