Skip to content

Switch 開關 ​

膠囊軌道 + 圓形滑塊的兩態切換控制項,支援載入中佔位。

契約源:lh-ui-core/specs/switch.json · 黃金用例:cases/switch.json(6 條,已翻譯為 JVM 單測 + Compose UI Test)

狀態 ​

開啟取主色軌道,關閉取描邊色軌道;滑塊直徑 = 高度 - 2 × switch.gap,位移動畫取 motion.duration.base。

9:41Switch
關閉
開啟
停用(關)
停用(開)
載入中(滑塊位置顯示載入環,不回應點選)

HTML 模擬預覽(Android / Compose 外觀),非真機渲染

kotlin
var on by remember { mutableStateOf(false) }
LhSwitch(checked = on, onChange = { on = it })

尺寸 ​

三檔尺寸對應到 switch.width / switch.height 令牌。

9:41Switch 尺寸

HTML 模擬預覽(Android / Compose 外觀),非真機渲染

API ​

Props ​

屬性說明類型預設值
checked是否開啟(必填)Boolean—
onChange開關態變更回呼,攜帶新開關態(Boolean) -> Unit—
disabled是否停用Booleanfalse
loading是否載入中(滑塊位置顯示載入環,不回應點選)Booleanfalse
lhSize尺寸LhSwitchSizeMedium
modifier修飾符ModifierModifier

LhSwitchSize ​

值widthheight
Smallswitch.width.small(36dp)switch.height.small(20dp)
Mediumswitch.width.medium(44dp)switch.height.medium(24dp)
Largeswitch.width.large(52dp)switch.height.large(28dp)

軌道為膠囊(圓角取高度一半);點選命中軌道區域,翻轉開關態並觸發 onChange。

令牌對照 ​

令牌用途
switch.width.{small,medium,large}三檔軌道寬度
switch.height.{small,medium,large}三檔軌道高度
switch.gap滑塊與軌道之間的留白(滑塊直徑 = 高度 - 2 × gap)
color.primary.default開啟軌道色
color.border.base關閉軌道色
color.onPrimary滑塊色
color.disabled.{bg,text}停用軌道 / 滑塊色
motion.duration.base / motion.easing.easeInOut滑塊位移動畫
loading.strokeWidth載入環描邊寬(複用)

黃金用例對照 ​

case id斷言本地測試
tap-toggles-and-emits-onchange點選翻轉並發射 onChangeLhSwitchTest.tap_unchecked_emits_true(Compose UI Test)
tap-checked-turns-off開啟態點選關閉LhSwitchTest.tap_checked_emits_false
disabled-blocks-change停用態不觸發LhSwitchTest.disabled_blocks_change
loading-blocks-change載入態不觸發LhSwitchTest.loading_blocks_change
checked-uses-primary-token開啟消費主色令牌LhSwitchDefaultsTest.checked_uses_primary_token
unchecked-uses-border-token關閉消費描邊令牌LhSwitchDefaultsTest.unchecked_uses_border_token
bash
./gradlew :components:switch:test                             # JVM 部分
./gradlew :components:switch:connectedAndroidTest            # 互動部分(需裝置)