-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic navigation drawer examples * Add previews * Fix merge issue * Apply Spotless * rearrange functions * Narrowing the examples to just the example with nested items * Apply Spotless * refactoring as dismissable drawer * Fixing imports * refactor, new region tags * Renaming functions * Apply Spotless * Add horizontal padding to the drawer content * Apply Spotless * Make drawer content scrollable to make it work on small screens / landscape --------- Co-authored-by: jakeroseman <[email protected]> Co-authored-by: Jolanda Verhoef <[email protected]>
- Loading branch information
1 parent
5545f37
commit 90b8500
Showing
3 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
compose/snippets/src/main/java/com/example/compose/snippets/components/NavigationDrawer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
import androidx.compose.foundation.layout.Spacer | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.automirrored.outlined.Help | ||
import androidx.compose.material.icons.filled.Menu | ||
import androidx.compose.material.icons.outlined.Settings | ||
import androidx.compose.material3.DrawerValue | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.HorizontalDivider | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.IconButton | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.ModalDrawerSheet | ||
import androidx.compose.material3.ModalNavigationDrawer | ||
import androidx.compose.material3.NavigationDrawerItem | ||
import androidx.compose.material3.Scaffold | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TopAppBar | ||
import androidx.compose.material3.rememberDrawerState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import kotlinx.coroutines.launch | ||
|
||
@Composable | ||
fun NavigationDrawerExamples() { | ||
// Add more examples here in future if necessary. | ||
|
||
DetailedDrawerExample { innerPadding -> | ||
Box(modifier = Modifier.padding(innerPadding)) { | ||
Text( | ||
"Swipe from left edge or use menu icon to open the dismissible drawer", | ||
modifier = Modifier.padding(16.dp) | ||
) | ||
} | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
// [START android_compose_components_detaileddrawerexample] | ||
@Composable | ||
fun DetailedDrawerExample( | ||
content: @Composable (PaddingValues) -> Unit | ||
) { | ||
val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed) | ||
val scope = rememberCoroutineScope() | ||
|
||
ModalNavigationDrawer( | ||
drawerContent = { | ||
ModalDrawerSheet { | ||
Column( | ||
modifier = Modifier.padding(horizontal = 16.dp) | ||
.verticalScroll(rememberScrollState()) | ||
) { | ||
Spacer(Modifier.height(12.dp)) | ||
Text("Drawer Title", modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleLarge) | ||
HorizontalDivider() | ||
|
||
Text("Section 1", modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium) | ||
NavigationDrawerItem( | ||
label = { Text("Item 1") }, | ||
selected = false, | ||
onClick = { /* Handle click */ } | ||
) | ||
NavigationDrawerItem( | ||
label = { Text("Item 2") }, | ||
selected = false, | ||
onClick = { /* Handle click */ } | ||
) | ||
|
||
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp)) | ||
|
||
Text("Section 2", modifier = Modifier.padding(16.dp), style = MaterialTheme.typography.titleMedium) | ||
NavigationDrawerItem( | ||
label = { Text("Settings") }, | ||
selected = false, | ||
icon = { Icon(Icons.Outlined.Settings, contentDescription = null) }, | ||
badge = { Text("20") }, // Placeholder | ||
onClick = { /* Handle click */ } | ||
) | ||
NavigationDrawerItem( | ||
label = { Text("Help and feedback") }, | ||
selected = false, | ||
icon = { Icon(Icons.AutoMirrored.Outlined.Help, contentDescription = null) }, | ||
onClick = { /* Handle click */ }, | ||
) | ||
Spacer(Modifier.height(12.dp)) | ||
} | ||
} | ||
}, | ||
drawerState = drawerState | ||
) { | ||
Scaffold( | ||
topBar = { | ||
TopAppBar( | ||
title = { Text("Navigation Drawer Example") }, | ||
navigationIcon = { | ||
IconButton(onClick = { | ||
scope.launch { | ||
if (drawerState.isClosed) { | ||
drawerState.open() | ||
} else { | ||
drawerState.close() | ||
} | ||
} | ||
}) { | ||
Icon(Icons.Default.Menu, contentDescription = "Menu") | ||
} | ||
} | ||
) | ||
} | ||
) { innerPadding -> | ||
content(innerPadding) | ||
} | ||
} | ||
} | ||
// [END android_compose_components_detaileddrawerexample] | ||
|
||
@Preview | ||
@Composable | ||
fun DetailedDrawerExamplePreview() { | ||
NavigationDrawerExamples() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters