Jetpack Compose Scaffold — Complete Guide
In this tutorial, you'll learn about Jetpack Compose Scaffold. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your Scaffold content overlaps with the top bar, the snackbar appears behind the bottom bar, or the FloatingActionButton covers the last list item.
Wrong Approach ❌
@Composable
fun BadScaffold() {
Scaffold(
topBar = { TopAppBar(title = { Text("Title") }) },
bottomBar = { BottomAppBar { Text("Bottom") } },
floatingActionButton = { FloatingActionButton(onClick = {}) { Icon(Icons.Default.Add, "") } }
) {
// content padding (it) is IGNORED
LazyColumn { items(100) { Text("Item $it") } }
}
}
Output: Content renders under the top bar and bottom bar. Last items hidden behind FAB.
Right Approach ✅
@Composable
fun GoodScaffold() {
val snackbarHostState = remember { SnackbarHostState() }
val scope = rememberCoroutineScope()
Scaffold(
topBar = {
TopAppBar(
title = { Text("App Name") },
actions = { IconButton(onClick = {}) { Icon(Icons.Default.Search, "") } }
)
},
bottomBar = {
NavigationBar {
NavigationBarItem(selected = true, onClick = {}, icon = { Icon(Icons.Default.Home, "") }, label = { Text("Home") })
}
},
floatingActionButton = {
FloatingActionButton(onClick = {
scope.launch { snackbarHostState.showSnackbar("Clicked!") }
}) {
Icon(Icons.Default.Add, "Add")
}
},
snackbarHost = { SnackbarHost(hostState = snackbarHostState) }
) { innerPadding ->
LazyColumn(
contentPadding = innerPadding // CRITICAL: apply scaffold padding
) {
items(100) { Text("Item $it", modifier = Modifier.fillMaxWidth().padding(16.dp)) }
}
}
}
Output: Content respects insets. Snackbar and FAB positioned correctly.
Prevention
- Always apply
innerPaddingto the outermost container of your content. - Use
SnackbarHostStatefor programmatic snackbar control. - Use
WindowInsetsAPIs for system bar insets (edge-to-edge). - Never hardcode padding — rely on Scaffold's computed insets.
Common Mistakes with compose scaffold
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
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