Surface
一个用于样式化内容容器的 Jetpack Compose Surface 组件。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
Expo UI Surface 与官方 Jetpack Compose Surface API 保持一致,并提供一个容器,用于应用 Material Design 的 Surface 样式,包括颜色、海拔和内容颜色。
安装
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
基础 Surface
import { Host, Surface, Text } from '@expo/ui/jetpack-compose'; import { paddingAll } from '@expo/ui/jetpack-compose/modifiers'; export default function BasicSurfaceExample() { return ( <Host matchContents> <Surface modifiers={[paddingAll(16)]}> <Text>Surface 上的内容</Text> </Surface> </Host> ); }
带海拔的 Surface
使用 tonalElevation 和 shadowElevation 来控制 Surface 的视觉深度。
import { Host, Surface, Column, Text } from '@expo/ui/jetpack-compose'; import { paddingAll } from '@expo/ui/jetpack-compose/modifiers'; export default function SurfaceElevationExample() { return ( <Host matchContents> <Column modifiers={[paddingAll(16)]}> <Surface tonalElevation={1} shadowElevation={2} modifiers={[paddingAll(16)]}> <Text>低海拔</Text> </Surface> <Surface tonalElevation={4} shadowElevation={8} modifiers={[paddingAll(16)]}> <Text>高海拔</Text> </Surface> </Column> </Host> ); }
带自定义颜色的 Surface
使用 color 和 contentColor 属性覆盖默认的 Material 主题颜色。
import { Host, Surface, Text } from '@expo/ui/jetpack-compose'; import { paddingAll } from '@expo/ui/jetpack-compose/modifiers'; export default function SurfaceCustomColorsExample() { return ( <Host matchContents> <Surface color="#1E3A5F" contentColor="#FFFFFF" tonalElevation={2} modifiers={[paddingAll(16)]}> <Text color="#FFFFFF">自定义颜色的 Surface</Text> </Surface> </Host> ); }
带形状和边框的 Surface
使用 shape 属性裁剪内容,并使用 border 在 Surface 周围绘制描边。
import { Host, Surface, Shape, Text } from '@expo/ui/jetpack-compose'; import { paddingAll } from '@expo/ui/jetpack-compose/modifiers'; export default function SurfaceShapeBorderExample() { return ( <Host matchContents> <Surface shape={Shape.RoundedCorner({ cornerRadii: { topStart: 16, topEnd: 16, bottomStart: 16, bottomEnd: 16 }, })} border={{ width: 2, color: '#6200EE' }} modifiers={[paddingAll(16)]}> <Text>带边框的圆角 Surface</Text> </Surface> </Host> ); }
API
import { Surface } from '@expo/ui/jetpack-compose';
Component
Type: React.Element<SurfaceProps>
A Material Design surface container. Surface is responsible for:
- Clipping content to the shape
- Applying background color based on tonal elevation
- Providing content color to its children
booleanWhether the surface is in a checked (toggled on) state. When provided together with
onCheckedChange, the surface becomes a toggleable surface.
ColorValueThe background color of the surface.
Defaults to MaterialTheme.colorScheme.surface.
ColorValueThe color of the content inside the surface.
Defaults to contentColorFor(color).
boolean • Default: trueWhether the surface is enabled and responds to user interaction.
(checked: boolean) => voidCalled when the checked state of a toggleable surface changes.
Providing this callback together with checked enables the toggleable variant.
() => voidCalled when the surface is clicked. Providing this callback makes the surface clickable.
When combined with selected, the surface becomes a selectable variant.
booleanWhether the surface is in a selected state. When provided together with onClick,
the surface becomes a selectable surface that visually reflects its selection state.
number • Default: 0The shadow elevation of the surface. Value in dp.
Types
Border stroke configuration.
| Property | Type | Description |
|---|---|---|
| color(optional) | ColorValue | Border color. Default: MaterialTheme.colorScheme.outline |
| width(optional) | number | Border width in dp. Default: 1 |