Skip to content

ECheckBox

ECheckBox 是一个带有动画效果的复选框组件,提供良好的用户交互体验。

基本用法

qml
import "components" as Components

Components.ETheme {
    id: theme
}

Components.ECheckBox {
    checked: false
    text: "同意用户协议"
    onCheckedChanged: console.log("复选框状态:", checked)
}

属性

属性名类型默认值描述
checkedboolfalse是否选中
textstring""复选框文本
widthrealauto复选框宽度,自动适应内容
heightreal24复选框高度
checkSizereal20复选框图标大小
textColorcolortheme.textPrimaryColor文本颜色
checkColorcolortheme.primaryColor复选框选中颜色
borderColorcolortheme.outlineColor复选框边框颜色
backgroundColorcolortheme.surfaceColor复选框背景颜色
disabledboolfalse是否禁用

方法

方法名参数返回值描述
toggle()void切换复选框状态
setChecked(checked)checked: boolvoid设置复选框选中状态

信号

信号名参数描述
checkedChangedchecked: bool复选框状态变化时触发
toggledchecked: bool复选框被点击切换状态时触发

示例

基本复选框

qml
ColumnLayout {
    spacing: 12
    
    Components.ECheckBox {
        text: "选项一"
        checked: true
    }
    
    Components.ECheckBox {
        text: "选项二"
    }
    
    Components.ECheckBox {
        text: "选项三(禁用)"
        disabled: true
    }
}

自定义样式的复选框

qml
Components.ECheckBox {
    text: "自定义样式"
    checkSize: 24
    checkColor: theme.secondaryColor
    borderColor: theme.secondaryColor
    textColor: theme.secondaryColor
    
    // 自定义复选框背景
    background: Rectangle {
        color: theme.surfaceVariantColor
        radius: 4
        width: parent.width
        height: parent.height
    }
}

复选框组

qml
ColumnLayout {
    spacing: 12
    
    property var selectedOptions: []
    
    function updateSelection(option, checked) {
        if (checked) {
            selectedOptions.push(option)
        } else {
            const index = selectedOptions.indexOf(option)
            if (index !== -1) {
                selectedOptions.splice(index, 1)
            }
        }
        console.log("选中的选项:", selectedOptions)
    }
    
    Components.ECheckBox {
        text: "苹果"
        onCheckedChanged: updateSelection(text, checked)
    }
    
    Components.ECheckBox {
        text: "香蕉"
        onCheckedChanged: updateSelection(text, checked)
    }
    
    Components.ECheckBox {
        text: "橙子"
        onCheckedChanged: updateSelection(text, checked)
    }
    
    Components.ECheckBox {
        text: "葡萄"
        onCheckedChanged: updateSelection(text, checked)
    }
}

注意事项

  • 复选框的动画效果使用 SpringAnimation 实现,提供流畅的过渡效果
  • 文本会自动换行以适应复选框宽度
  • 禁用状态下,复选框会自动降低透明度并禁用交互

使用 VitePress 构建