开关
用于切换控件的 Jetpack Compose 开关组件。
Expo UI 开关与官方 Jetpack Compose 的 Switch 和 Checkbox API 相匹配,并支持开关、复选框和按钮变体。
🌐 Expo UI Switch matches the official Jetpack Compose Switch and Checkbox APIs and supports switch, checkbox, and button variants.
安装
🌐 Installation
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
🌐 Usage
切换开关
🌐 Toggle switch
使用 variant="switch" 属性来显示标准切换开关。
🌐 Use the variant="switch" prop to display a standard toggle switch.
import { useState } from 'react'; import { Host, Switch } from '@expo/ui/jetpack-compose'; export default function ToggleSwitchExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Switch value={checked} onValueChange={setChecked} label="Play music" variant="switch" color="#6200EE" /> </Host> ); }
复选框
🌐 Checkbox
使用 variant="checkbox" 属性来显示一个复选框控件。
🌐 Use the variant="checkbox" prop to display a checkbox control.
import { useState } from 'react'; import { Host, Switch } from '@expo/ui/jetpack-compose'; export default function CheckboxExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Switch value={checked} onValueChange={setChecked} label="Accept terms" variant="checkbox" color="#6200EE" /> </Host> ); }
按钮变体
🌐 Button variant
使用 variant="button" 属性来显示一个切换按钮。
🌐 Use the variant="button" prop to display a toggle button.
import { useState } from 'react'; import { Host, Switch } from '@expo/ui/jetpack-compose'; export default function ButtonVariantExample() { const [checked, setChecked] = useState(false); return ( <Host matchContents> <Switch value={checked} onValueChange={setChecked} label="Notifications" variant="button" color="#6200EE" /> </Host> ); }
应用接口
🌐 API
import { Switch } from '@expo/ui/jetpack-compose';
Components
Type: React.Element<SwitchProps>
stringLabel for the switch.
On Android, the label has an effect only when the
Switchis used inside aContextMenu.
(value: boolean) => voidCallback function that is called when the checked state changes.
Type: React.Element<ThumbContentProps>
Custom content to be displayed inside the switch thumb.