Skip to content

Material ​

Use Material to share a sampled backdrop across screens, panels, and cards. Set the style in SaltTheme, place the source pixels in MaterialSource, and apply Modifier.material() or Modifier.subMaterial() to consumers. These SaltUI APIs are based on Haze and require @OptIn(UnstableSaltUiApi::class). For interactive refraction on individual controls, see Glass. For menus, see PopupMenuBackdrop.

Complete example ​

The example places a wallpaper behind a primary background and a secondary panel. Supply the wallpaper Painter. The button count is foreground state and can change independently.

kotlin
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import com.moriafly.salt.ui.Button
import com.moriafly.salt.ui.SaltMaterial
import com.moriafly.salt.ui.SaltTheme
import com.moriafly.salt.ui.Text
import com.moriafly.salt.ui.UnstableSaltUiApi
import com.moriafly.salt.ui.innerPadding
import com.moriafly.salt.ui.material.MaterialSource
import com.moriafly.salt.ui.material.MaterialType
import com.moriafly.salt.ui.material.material
import com.moriafly.salt.ui.material.subMaterial

@OptIn(UnstableSaltUiApi::class)
@Composable
fun WallpaperPage(
    wallpaper: Painter,
    modifier: Modifier = Modifier
) {
    var clicks by rememberSaveable { mutableIntStateOf(0) }

    SaltTheme(
        material = SaltMaterial(type = MaterialType.Mica)
    ) {
        Box(
            modifier = modifier.fillMaxSize()
        ) {
            MaterialSource(
                modifier = Modifier.fillMaxSize(),
                materialSelf = false
            ) {
                Image(
                    painter = wallpaper,
                    contentDescription = null,
                    modifier = Modifier.fillMaxSize(),
                    contentScale = ContentScale.Crop
                )
            }

            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .material(fallback = SaltTheme.colors.background)
                    .innerPadding()
            ) {
                Text(text = "Material screen")

                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .clip(SaltTheme.shapes.medium)
                        .subMaterial(fallback = SaltTheme.colors.subBackground)
                        .innerPadding()
                ) {
                    Button(
                        onClick = { clicks++ },
                        text = "Clicks: $clicks"
                    )
                }
            }
        }
    }
}

Choose a material ​

Pass SaltMaterial(type = ...) to SaltTheme.

TypeAppearance
NoneDraws the modifier’s fallback color. This is the default.
BlurryGlassBlur, light or dark tint, and subtle noise.
AcrylicAcrylic-style material with separate primary and secondary presets.
MicaMica-style material with separate primary and secondary presets. It samples the backdrop you draw.
PremiumStronger blur and layered color mixing, with subtle noise on the secondary surface.

Source and drawing order ​

Put a wallpaper, gradient, or video inside MaterialSource. Lay out material consumers after the source, usually as siblings in the same Box. Give the source and consumers a real size.

text
SaltTheme
└─ Box
   ├─ MaterialSource → Wallpaper / background
   └─ Page content
      ├─ material() → Primary background
      └─ subMaterial() → Card / panel

materialSelf ​

With the default materialSelf = true, the source draws a primary material layer over itself. Set it to false to keep the wallpaper clear for local panels or when the foreground container already uses material().

Primary and secondary surfaces ​

material() uses MaterialLayer.Background. subMaterial() uses MaterialLayer.SubBackground. Both sample the original backdrop. Place the clip before the material modifier and padding afterward.

kotlin
Modifier
    .clip(SaltTheme.shapes.medium)
    .subMaterial(fallback = SaltTheme.colors.subBackground)
    .innerPadding()

Built-in containers ​

Card, RoundedColumn, and ScreenCard already apply secondary material when given a background color. Set color = Color.Unspecified to skip a container’s own backdrop. Other options, such as stroke, are independent.

kotlin
ScreenCard(
    header = "Playback"
) {
    ItemButton(
        onClick = onOpenEqualizer,
        text = "Equalizer"
    )
}

Theme scope ​

Place source and consumers in the same SaltTheme scope. A nested theme has its own material source. Use theme configuration to change both the material and foreground colors. Setting only a material modifier's isDarkTheme changes only that effect.

Disable material or use a fallback ​

Use DisableMaterial for a subtree that needs its own opaque backdrop and supply a fallback color.

kotlin
DisableMaterial {
    Box(
        modifier = Modifier
            .clip(SaltTheme.shapes.medium)
            .subMaterial(fallback = SaltTheme.colors.popup)
            .innerPadding()
    ) {
        Text(text = "Standalone panel")
    }
}

MaterialType.None and DisableMaterial draw the modifier’s fallback, which defaults to Color.Unspecified. Check source scope and dimensions when there is nothing to sample. To draw a solid surface, disable material and supply a fallback color.

Custom Haze styles ​

SaltHazeStyles.blurryGlass, acrylic, mica, and premium return presets by layer and theme for a custom Haze source.

Troubleshooting ​

The current main branch uses Haze for backdrop sampling. The static cache described in earlier docs came from a separate experimental branch.

SymptomCheck
No material effectConfirm the style, scope, source size, and that the source actually draws pixels.
Material looks too strongCheck whether materialSelf overlaps a foreground material(), or a card repeats subMaterial().
Text colors clash with a dark materialSet the whole theme to dark so the foreground matches.

Open Theme and material from the Sample home screen to try the available materials. See the Material source for declarations.

Google/Android 是 Google LLC 的注册商标,Salt Player 和糖醋音乐均是寻浔科技(上海)有限公司在中华人民共和国的注册商标