Skip to the workspace
Pebellslearn by building
Navigation and App Structure  ›  Lesson

Scaffold and the Navigation Drawer

Drawer 15 minScaffold arranges the standard parts of a screen, such as top bar, floating action button and content, and a modal navigation drawer slides in a menu of top-level destinations.

android / Scaffold and the Navigation Drawer

Explore each scene at your own pace.

01 · The rule

The rule

Scaffold and the Navigation Drawer

Almost every app screen has the same frame: a bar at the top, content in the middle, maybe a round "+" button in the corner and a menu that slides in from the side. Placing all of that by hand is fiddly. Compose gives you Scaffold for the frame and ModalNavigationDrawer for the side menu.

The rule

Scaffold fills the slots; content honours the padding.

Scaffold has named slots for the standard parts of a screen:

Scaffold(
    topBar = {
        TopAppBar(
            title = { Text("My plants") },
            navigationIcon = {
                IconButton(onClick = { scope.launch { drawerState.open() } }) {
                    Icon(Icons.Filled.Menu, contentDescription = "Open menu")
                }
            }
        )
    },
    floatingActionButton = {
        FloatingActionButton(onClick = onAdd) {
            Icon(Icons.Filled.Add, contentDescription = "Add plant")
        }
    }
) { innerPadding ->
    PlantList(Modifier.padding(innerPadding))
}

The content lambda receives innerPadding: the space the bars take up. You must apply it, or your first list item hides under the top bar.

A navigation drawer holds the app's top-level destinations. ModalNavigationDrawer wraps the screen and slides over it when opened:

val drawerState = rememberDrawerState(DrawerValue.Closed)
val scope = rememberCoroutineScope()

ModalNavigationDrawer(
    drawerState = drawerState,
    drawerContent = { ModalDrawerSheet { SproutMenuItems() } }
) {
    Scaffold(/* as above */) { /* ... */ }
}

open() and close() animate the drawer, so they are suspend functions, launched from a coroutine scope.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.