Jetpack Compose Drawer — Complete Guide
In this tutorial, you'll learn about Jetpack Compose Drawer. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your drawer opens on a swipe but the hamburger icon doesn't reflect the state, or the drawer content overlaps the main content.
Wrong Approach ❌
@Composable
fun BadDrawer() {
// No drawer state — always closed
ModalNavigationDrawer(drawerContent = { Text("Drawer") }) {
// Main content
}
}
@Composable
fun GestureConflict() {
ModalNavigationDrawer(
drawerContent = { Text("Drawer") },
gesturesEnabled = true
) {
LazyColumn { items(50) { Text("Item $it") } } // Scroll conflicts with drawer
}
}
Output: Drawer never opens or opens accidentally during list scroll.
Right Approach ✅
@Composable
fun GoodDrawer() {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()
val isOpen by remember { derivedStateOf { drawerState.isOpen } }
ModalNavigationDrawer(
drawerState = drawerState,
gesturesEnabled = drawerState.isOpen || true,
drawerContent = {
ModalDrawerSheet {
// Drawer header
Box(
modifier = Modifier
.fillMaxWidth()
.height(180.dp)
.background(MaterialTheme.colorScheme.primaryContainer)
) {
Column(modifier = Modifier.align(Alignment.BottomStart).padding(16.dp)) {
Text("User Name", style = MaterialTheme.typography.headlineSmall)
Text("email@example.com", style = MaterialTheme.typography.bodyMedium)
}
}
// Navigation items
NavigationDrawerItem(label = { Text("Home") }, selected = true, onClick = {
scope.launch { drawerState.close() }
})
NavigationDrawerItem(label = { Text("Settings") }, selected = false, onClick = {
scope.launch { drawerState.close() }
})
}
}
) {
Scaffold(
topBar = {
TopAppBar(
title = { Text("App") },
navigationIcon = {
IconButton(onClick = {
scope.launch {
if (drawerState.isClosed) drawerState.open()
else drawerState.close()
}
}) {
Icon(Icons.Default.Menu, "Menu")
}
}
)
}
) { padding ->
MainContent(Modifier.padding(padding))
}
}
}
Output: Drawer opens on hamburger click or gesture, state synced with icon.
Prevention
- Use
rememberDrawerState()withDrawerValue.Closedinitial state. - Use
ModalDrawerSheetfor drawer content with proper styling. - Handle hamburger icon toggling via
drawerState.isOpen. - Disable
gesturesEnabledwhen scrollable content is in the drawer.
Common Mistakes with compose drawer
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro