State of Compose 2023 results are in! Click here to learn more

← Back to Material 3 Compose

Surface

Component
in
Material 3
. Since 1.0.0

Overview

Code Examples

Material surface is the central metaphor in material design. Each surface exists at a given elevation, which influences how that piece of surface visually relates to other surfaces and how that surface is modified by tonal variance.

See the other overloads for clickable, selectable, and toggleable surfaces.

The Surface is responsible for:

  1. Clipping: Surface clips its children to the shape specified by shape

  2. Borders: If shape has a border, then it will also be drawn.

  3. Background: Surface fills the shape specified by shape with the color. If color is ColorScheme.surface a color overlay will be applied. The color of the overlay depends on the tonalElevation of this Surface, and the LocalAbsoluteTonalElevation set by any parent surfaces. This ensures that a Surface never appears to have a lower elevation overlay than its ancestors, by summing the elevation of all previous Surfaces.

  4. Content color: Surface uses contentColor to specify a preferred color for the content of this surface - this is used by the Text and Icon components as a default color.

If no contentColor is set, this surface will try and match its background color to a color defined in the theme ColorScheme, and return the corresponding content color. For example, if the color of this surface is ColorScheme.surface, contentColor will be set to ColorScheme.onSurface. If color is not part of the theme palette, contentColor will keep the same value set above this Surface.

To manually retrieve the content color inside a surface, use LocalContentColor.

  1. Blocking touch propagation behind the surface.

Surface sample:

Overloads

Surface

@Composable
@NonRestartableComposable
fun Surface(
    modifier: Modifier = Modifier,
    shape: Shape = RectangleShape,
    color: Color = MaterialTheme.colorScheme.surface,
    contentColor: Color = contentColorFor(color),
    tonalElevation: Dp = 0.dp,
    shadowElevation: Dp = 0.dp,
    border: BorderStroke? = null,
    content: @Composable () -> Unit
)

Parameters

NameDescription
modifierModifier to be applied to the layout corresponding to the surface
shapeDefines the surface's shape as well its shadow.
colorThe background color. Use Color.Transparent to have no color.
contentColorThe preferred content color provided by this Surface to its children. Defaults to either the matching content color for color, or if color is not a color from the theme, this will keep the same value set above this Surface.
tonalElevationWhen color is ColorScheme.surface, a higher the elevation will result in a darker color in light theme and lighter color in dark theme.
shadowElevationThe size of the shadow below the surface. To prevent shadow creep, only apply shadow elevation when absolutely necessary, such as when the surface requires visual separation from a patterned background. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use Modifier.zIndex.
borderOptional border to draw on top of the surfac

Surface

@Composable
@NonRestartableComposable
fun Surface(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shape: Shape = RectangleShape,
    color: Color = MaterialTheme.colorScheme.surface,
    contentColor: Color = contentColorFor(color),
    tonalElevation: Dp = 0.dp,
    shadowElevation: Dp = 0.dp,
    border: BorderStroke? = null,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    content: @Composable () -> Unit
)

Parameters

NameDescription
onClickcallback to be called when the surface is clicked
modifierModifier to be applied to the layout corresponding to the surface
enabledControls the enabled state of the surface. When false, this surface will not be clickable
shapeDefines the surface's shape as well its shadow. A shadow is only displayed if the tonalElevation is greater than zero.
colorThe background color. Use Color.Transparent to have no color.
contentColorThe preferred content color provided by this Surface to its children. Defaults to either the matching content color for color, or if color is not a color from the theme, this will keep the same value set above this Surface.
borderOptional border to draw on top of the surface
tonalElevationWhen color is ColorScheme.surface, a higher the elevation will result in a darker color in light theme and lighter color in dark theme.
shadowElevationThe size of the shadow below the surface. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use Modifier.zIndex.
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this Surface. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this Surface in different Interactions

Surface

@Composable
@NonRestartableComposable
fun Surface(
    selected: Boolean,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shape: Shape = RectangleShape,
    color: Color = MaterialTheme.colorScheme.surface,
    contentColor: Color = contentColorFor(color),
    tonalElevation: Dp = 0.dp,
    shadowElevation: Dp = 0.dp,
    border: BorderStroke? = null,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    content: @Composable () -> Unit
)

Parameters

NameDescription
selectedwhether or not this Surface is selected
onClickcallback to be called when the surface is clicked
modifierModifier to be applied to the layout corresponding to the surface
enabledControls the enabled state of the surface. When false, this surface will not be clickable
shapeDefines the surface's shape as well its shadow. A shadow is only displayed if the tonalElevation is greater than zero.
colorThe background color. Use Color.Transparent to have no color.
contentColorThe preferred content color provided by this Surface to its children. Defaults to either the matching content color for color, or if color is not a color from the theme, this will keep the same value set above this Surface.
borderOptional border to draw on top of the surface
tonalElevationWhen color is ColorScheme.surface, a higher the elevation will result in a darker color in light theme and lighter color in dark theme.
shadowElevationThe size of the shadow below the surface. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use Modifier.zIndex.
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this Surface. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this Surface in different Interactions

Surface

@Composable
@NonRestartableComposable
fun Surface(
    checked: Boolean,
    onCheckedChange: (Boolean) -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    shape: Shape = RectangleShape,
    color: Color = MaterialTheme.colorScheme.surface,
    contentColor: Color = contentColorFor(color),
    tonalElevation: Dp = 0.dp,
    shadowElevation: Dp = 0.dp,
    border: BorderStroke? = null,
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    content: @Composable () -> Unit
)

Parameters

NameDescription
checkedwhether or not this Surface is toggled on or off
onCheckedChangecallback to be invoked when the toggleable Surface is clicked
modifierModifier to be applied to the layout corresponding to the surface
enabledControls the enabled state of the surface. When false, this surface will not be clickable
shapeDefines the surface's shape as well its shadow. A shadow is only displayed if the tonalElevation is greater than zero.
colorThe background color. Use Color.Transparent to have no color.
contentColorThe preferred content color provided by this Surface to its children. Defaults to either the matching content color for color, or if color is not a color from the theme, this will keep the same value set above this Surface.
borderOptional border to draw on top of the surface
tonalElevationWhen color is ColorScheme.surface, a higher the elevation will result in a darker color in light theme and lighter color in dark theme.
shadowElevationThe size of the shadow below the surface. Note that It will not affect z index of the Surface. If you want to change the drawing order you can use Modifier.zIndex.
interactionSourcethe MutableInteractionSource representing the stream of Interactions for this Surface. You can create and pass in your own remembered MutableInteractionSource if you want to observe Interactions and customize the appearance / behavior of this Surface in different Interactions