This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

ToggleButton

Jetpack Compose ToggleButton 组件用于显示原生 Material3 切换按钮。

Android
Included in Expo Go
Recommended version:
~57.0.3

Expo UI 提供四个切换按钮组件,这些组件与官方 Jetpack Compose 切换按钮 API 相匹配:ToggleButtonIconToggleButtonFilledIconToggleButtonOutlinedIconToggleButton

🌐 Expo UI provides four toggle button components that match the official Jetpack Compose Toggle Button API: ToggleButton, IconToggleButton, FilledIconToggleButton, and OutlinedIconToggleButton.

A toggle button with text and two icon toggle buttons in checked and unchecked states

安装

🌐 Installation

Terminal
npx expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

🌐 Usage

基本切换按钮

🌐 Basic toggle button

一个带有文本和图标内容的切换按钮。

🌐 A toggle button with text and icon content.

BasicToggleButtonExample.tsx
import { useState } from 'react'; import { Host, ToggleButton, Text } from '@expo/ui/jetpack-compose'; export default function BasicToggleButtonExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <ToggleButton checked={checked} onCheckedChange={setChecked}> <Text>Favorite</Text> </ToggleButton> </Host> ); }

图标切换按钮变体

🌐 Icon toggle button variants

使用不同的图标切换按钮组件来传达不同程度的强调。

🌐 Use different icon toggle button components to convey varying levels of emphasis.

IconToggleButtonVariantsExample.tsx
import { useState } from 'react'; import { Host, IconToggleButton, FilledIconToggleButton, OutlinedIconToggleButton, Icon, Row, } from '@expo/ui/jetpack-compose'; const starIcon = require('./assets/star.png'); export default function IconToggleButtonVariantsExample() { const [checked1, setChecked1] = useState(false); const [checked2, setChecked2] = useState(true); const [checked3, setChecked3] = useState(false); return ( <Host matchContents> <Row horizontalArrangement={{ spacedBy: 8 }}> <IconToggleButton checked={checked1} onCheckedChange={setChecked1}> <Icon source={starIcon} size={24} /> </IconToggleButton> <FilledIconToggleButton checked={checked2} onCheckedChange={setChecked2}> <Icon source={starIcon} size={24} /> </FilledIconToggleButton> <OutlinedIconToggleButton checked={checked3} onCheckedChange={setChecked3}> <Icon source={starIcon} size={24} /> </OutlinedIconToggleButton> </Row> </Host> ); }

自定义颜色

🌐 Custom colors

使用 colors 属性覆盖选中和未选中时的颜色。

🌐 Override checked and unchecked colors using the colors prop.

CustomColorsToggleButtonExample.tsx
import { useState } from 'react'; import { Host, ToggleButton, Text } from '@expo/ui/jetpack-compose'; export default function CustomColorsToggleButtonExample() { const [checked, setChecked] = useState(true); return ( <Host matchContents> <ToggleButton checked={checked} onCheckedChange={setChecked} colors={{ checkedContainerColor: '#4CAF50', checkedContentColor: '#FFFFFF', containerColor: '#E0E0E0', contentColor: '#333333', }}> <Text>{checked ? 'ON' : 'OFF'}</Text> </ToggleButton> </Host> ); }

禁用切换按钮

🌐 Disabled toggle button

DisabledToggleButtonExample.tsx
import { Host, ToggleButton, Text } from '@expo/ui/jetpack-compose'; export default function DisabledToggleButtonExample() { return ( <Host matchContents> <ToggleButton checked={false} enabled={false}> <Text>Disabled</Text> </ToggleButton> </Host> ); }

应用接口

🌐 API

import { ToggleButton, IconToggleButton, FilledIconToggleButton, OutlinedIconToggleButton, } from '@expo/ui/jetpack-compose';

Components

FilledIconToggleButton

Type: React.Element<ToggleButtonProps>

A filled icon toggle button with a solid background.

IconToggleButton

Type: React.Element<ToggleButtonProps>

An icon toggle button with no background.

OutlinedIconToggleButton

Type: React.Element<ToggleButtonProps>

An outlined icon toggle button with a border and no fill.

ToggleButton

Type: React.Element<ToggleButtonProps>

A toggle button component that can be toggled on and off.

ToggleButtonProps

checked

Type: boolean

Whether the toggle button is checked.

children

Type: ReactNode

Content to display inside the toggle button.

colors

Optional • Type: ToggleButtonColors

Colors for toggle button elements.

enabled

Optional • Type: boolean • Default: true

Whether the button is enabled for user interaction.

modifiers

Optional • Type: ModifierConfig[]

Modifiers for the component.

onCheckedChange

Optional • Type: (checked: boolean) => void

Callback that is called when the checked state changes.

ToggleButton.DefaultIconSize

Type: React.Element<number>

Types

ToggleButtonColors

Colors for toggle button elements.

PropertyTypeDescription
checkedContainerColor(optional)ColorValue
-
checkedContentColor(optional)ColorValue
-
containerColor(optional)ColorValue
-
contentColor(optional)ColorValue
-
disabledContainerColor(optional)ColorValue
-
disabledContentColor(optional)ColorValue
-