Composables
Docs Login
Tally

Screen Scaffold

Jetpack Compose Component

Download
package com.composables.tally.components

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.layout.SubcomposeLayout
import com.composables.systembars.SystemBarContent
import com.composables.systembars.SystemBars
import com.composables.tally.theme.ProvideContentColor
import com.composables.tally.theme.Theme
import com.composeunstyled.DialogHost

@Composable
fun ScreenScaffold(
    modifier: Modifier = Modifier,
    bottomBar: @Composable () -> Unit = {},
    content: @Composable BoxScope.(contentPadding: PaddingValues) -> Unit,
) {
    SystemBars(all = systemBarContentOn(Theme.colors.background))
    DialogHost(modifier.fillMaxSize()) {
        ProvideContentColor(Theme.colors.onBackground) {
            SubcomposeLayout(Modifier.fillMaxSize().background(Theme.colors.background)) {
                constraints ->
                val bar =
                    subcompose(Slot.BottomBar, bottomBar).map {
                        it.measure(constraints.copy(minHeight = 0))
                    }
                val barHeight = bar.maxOfOrNull { it.height } ?: 0
                val body =
                    subcompose(Slot.Content) {
                            Box(Modifier.fillMaxSize()) {
                                content(PaddingValues(bottom = barHeight.toDp()))
                            }
                        }
                        .map { it.measure(constraints) }
                layout(constraints.maxWidth, constraints.maxHeight) {
                    body.forEach { it.place(0, 0) }
                    bar.forEach { it.place(0, constraints.maxHeight - it.height) }
                }
            }
        }
    }
}

private enum class Slot {
    BottomBar,
    Content,
}

private fun systemBarContentOn(background: Color): SystemBarContent =
    if (background.luminance() > 0.5f) SystemBarContent.Dark else SystemBarContent.Light

More components from Tally

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.