/
MaterialLayoutSnippets.kt
410 lines (391 loc) · 13.2 KB
/
MaterialLayoutSnippets.kt
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright 2023 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.layouts
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Image
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FabPosition
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.SnackbarResult
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
// [START android_compose_layout_material_theme]
@Composable
fun MyApp() {
MaterialTheme {
// Material Components like Button, Card, Switch, etc.
}
}
// [END android_compose_layout_material_theme]
@Composable
fun ButtonDemo() {
// [START android_compose_layout_material_button]
Button(
onClick = { /* ... */ },
// Uses ButtonDefaults.ContentPadding by default
contentPadding = PaddingValues(
start = 20.dp,
top = 12.dp,
end = 20.dp,
bottom = 12.dp
)
) {
// Inner content including an icon and a text label
Icon(
Icons.Filled.Favorite,
contentDescription = "Favorite",
modifier = Modifier.size(ButtonDefaults.IconSize)
)
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
Text("Like")
}
// [END android_compose_layout_material_button]
}
@Composable
fun ExtendedFabDemo() {
// [START android_compose_layout_material_fab]
ExtendedFloatingActionButton(
onClick = { /* ... */ },
icon = {
Icon(
Icons.Filled.Favorite,
contentDescription = "Favorite"
)
},
text = { Text("Like") }
)
// [END android_compose_layout_material_fab]
}
@Composable
fun ScaffoldDemo() {
// [START android_compose_layout_material_scaffold]
Scaffold(/* ... */) { contentPadding ->
// Screen content
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
}
// [END android_compose_layout_material_scaffold]
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ScaffoldTopAppBarDemo() {
// [START android_compose_layout_material_appbar]
Scaffold(
topBar = {
TopAppBar(title = {
Text("My App")
})
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_appbar]
}
@Composable
fun ScaffoldBottomBarDemo() {
// [START android_compose_layout_material_bottombar]
Scaffold(
bottomBar = {
BottomAppBar { /* Bottom app bar content */ }
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_bottombar]
}
@Composable
fun ScaffoldFabDemo() {
// [START android_compose_layout_material_scaffold_fab]
Scaffold(
floatingActionButton = {
FloatingActionButton(onClick = { /* ... */ }) {
/* FAB content */
}
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_scaffold_fab]
}
@Composable
fun ScaffoldFabPositionDemo() {
// [START android_compose_layout_material_scaffold_fab_position]
Scaffold(
floatingActionButton = {
FloatingActionButton(onClick = { /* ... */ }) {
/* FAB content */
}
},
floatingActionButtonPosition = FabPosition.Center
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_scaffold_fab_position]
}
@Composable
fun ScaffoldFabAndBottomBarDemo() {
// [START android_compose_layout_material_scaffold_fab_docked]
Scaffold(
floatingActionButton = {
FloatingActionButton(onClick = { /* ... */ }) {
/* FAB content */
}
},
bottomBar = {
BottomAppBar { /* Bottom app bar content */ }
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_scaffold_fab_docked]
}
@Composable
fun ScaffoldSnackbarDemo() {
// [START android_compose_layout_material_snackbar]
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
Scaffold(
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
floatingActionButton = {
ExtendedFloatingActionButton(
text = { Text("Show snackbar") },
icon = { Icon(Icons.Filled.Image, contentDescription = "") },
onClick = {
scope.launch {
snackbarHostState.showSnackbar("Snackbar")
}
}
)
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_snackbar]
}
@Composable
fun ScaffoldSnackbarResultDemo() {
// [START android_compose_layout_material_snackbar_result]
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }
Scaffold(
snackbarHost = {
SnackbarHost(hostState = snackbarHostState)
},
floatingActionButton = {
ExtendedFloatingActionButton(
text = { Text("Show snackbar") },
icon = { Icon(Icons.Filled.Image, contentDescription = "") },
onClick = {
scope.launch {
val result = snackbarHostState
.showSnackbar(
message = "Snackbar",
actionLabel = "Action",
// Defaults to SnackbarDuration.Short
duration = SnackbarDuration.Indefinite
)
when (result) {
SnackbarResult.ActionPerformed -> {
/* Handle snackbar action performed */
}
SnackbarResult.Dismissed -> {
/* Handle snackbar dismissed */
}
}
}
}
)
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
// [END android_compose_layout_material_snackbar_result]
}
@Composable
fun DrawerDemo() {
// [START android_compose_layout_material_modal_drawer]
ModalNavigationDrawer(
drawerContent = {
ModalDrawerSheet {
Text("Drawer title", modifier = Modifier.padding(16.dp))
Divider()
NavigationDrawerItem(
label = { Text(text = "Drawer Item") },
selected = false,
onClick = { /*TODO*/ }
)
// ...other drawer items
}
}
) {
// Screen content
}
// [END android_compose_layout_material_modal_drawer]
}
@Composable
fun DrawerGesturesDemo() {
// [START android_compose_layout_material_modal_drawer_gestures]
ModalNavigationDrawer(
drawerContent = {
ModalDrawerSheet {
// Drawer contents
}
},
gesturesEnabled = false
) {
// Screen content
}
// [END android_compose_layout_material_modal_drawer_gestures]
}
@Composable
fun DrawerStateDemo() {
// [START android_compose_layout_material_modal_drawer_programmatic]
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val scope = rememberCoroutineScope()
ModalNavigationDrawer(
drawerState = drawerState,
drawerContent = {
ModalDrawerSheet { /* Drawer content */ }
},
) {
Scaffold(
floatingActionButton = {
ExtendedFloatingActionButton(
text = { Text("Show drawer") },
icon = { Icon(Icons.Filled.Add, contentDescription = "") },
onClick = {
scope.launch {
drawerState.apply {
if (isClosed) open() else close()
}
}
}
)
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
}
}
// [END android_compose_layout_material_modal_drawer_programmatic]
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun BottomSheetDemo() {
// [START android_compose_layout_material_bottom_sheet]
ModalBottomSheet(onDismissRequest = { /* Executed when the sheet is dismissed */ }) {
// Sheet content
}
// [END android_compose_layout_material_bottom_sheet]
// [START android_compose_layout_material_bottom_sheet2]
val sheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()
var showBottomSheet by remember { mutableStateOf(false) }
Scaffold(
floatingActionButton = {
ExtendedFloatingActionButton(
text = { Text("Show bottom sheet") },
icon = { Icon(Icons.Filled.Add, contentDescription = "") },
onClick = {
showBottomSheet = true
}
)
}
) { contentPadding ->
// Screen content
// [START_EXCLUDE silent]
Box(modifier = Modifier.padding(contentPadding)) { /* ... */ }
// [END_EXCLUDE]
if (showBottomSheet) {
ModalBottomSheet(
onDismissRequest = {
showBottomSheet = false
},
sheetState = sheetState
) {
// Sheet content
Button(onClick = {
scope.launch { sheetState.hide() }.invokeOnCompletion {
if (!sheetState.isVisible) {
showBottomSheet = false
}
}
}) {
Text("Hide bottom sheet")
}
}
}
}
// [END android_compose_layout_material_bottom_sheet2]
}