Skip to content

Switch 开关

介绍

用于在打开和关闭状态之间进行切换。

TIP

阅读该组件文档前请确保已认真阅读快速上手章节的每一个字

引入

ts
import { IBestSwitch } from "@ibestservices/ibest-ui-v2";

代码演示

基础用法

基础用法

TIP

通过 value 来设置开关的状态,通过 onChange 监听状态变化。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value: boolean = false
  build() {
    Column(){
      IBestSwitch({
        value: this.value!!
      })
    }
  }
}

自定义尺寸

自定义尺寸

TIP

通过 switchSize 属性自定义开关的大小。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value: boolean = true
  build() {
    Column({space: 20}){
      IBestSwitch({
        value: this.value!!,
        switchSize: 20,
        componentWidth: 60
      })
      IBestSwitch({
        value: this.value!!,
        componentPadding: 4
      })
    }
  }
}

文字图标

文字图标

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value: boolean = true
  build() {
    Column({space: 20}){
      IBestSwitch({
        value: this.value!!,
        inactiveText: "OFF",
        activeText: "ON",
        componentWidth: 70
      })
      IBestSwitch({
        value: this.value!!,
        inactiveIcon: $r("app.media.icon_dark"),  // 替换为自己项目图片
        activeIcon: $r("app.media.icon_light"),   // 替换为自己项目图片
        activeTextColor: "#f0ff86"
      })
    }
  }
}

扩展value类型

扩展value类型

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local stringValue: string = 'off'
  @Local numberValue: number = 1
  build() {
    Column({space: 20}){
      Row({space: 14}){
        Text("string")
        IBestSwitch({
          value: this.stringValue!!,
          inactiveValue: "off",
          activeValue: "on"
        })
        Text(this.stringValue)
      }
      Row({space: 14}){
        Text("number")
        IBestSwitch({
          value: this.numberValue!!,
          inactiveValue: 0,
          activeValue: 1
        })
        Text(this.numberValue.toString())
      }
    }
  }
}

禁用状态

禁用状态

TIP

通过 disabled 属性来禁用开关,禁用状态下开关不可点击。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value1: boolean = true
  @Local value2: boolean = false
  build() {
    Column({space: 14}){
      IBestSwitch({
        value: this.value1!!,
        disabled: true
      })
      IBestSwitch({
        value: this.value2!!,
        disabled: true
      })
    }
  }
}

加载状态

加载状态

TIP

通过 loading 属性来禁用开关,加载状态下开关不可点击。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value1: boolean = true
  @Local value2: boolean = false
  build() {
    Column({space: 14}){
      IBestSwitch({
        value: this.value1!!,
        loading: true
      })
      IBestSwitch({
        value: this.value2!!,
        loading: true,
        activeColor: '#07c160'
      })
    }
  }
}

自定义按钮

自定义按钮

TIP

通过 nodeBuilder 插槽自定义按钮的内容。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value: boolean = true
  @Local arrowDirection: 'left' | 'right' = 'left'
  @Builder Arrow(){
    Row(){
      Image($r('app.media.arrow'))
        .width(15)
        .fillColor(this.arrowDirection === 'left' ? '#db3131' : '#e2e3e7')
        .rotate({
          angle: this.arrowDirection === 'left' ? 0 : -180
        }).animation({
          duration: 200
        })
    }
  }
  build(){
    Column(){
      IBestSwitch({
        value: this.value!!,
        activeColor: '#db3131',
        nodeBuilder: () => this.Arrow(),
        onChange: value => {
          this.arrowDirection = value ? 'left' : 'right'
        }
      })
    }
  }
}

异步控制

异步控制

TIP

onBeforeChange 事件的回调函数中返回Promise<false>false, 可阻止按钮状态的变化。

点我查看代码
ts
@Entry
@ComponentV2
struct DemoPage {
  @Local value: boolean = true
  build() {
    Column(){
      IBestSwitch({
        value: this.value!!,
        onBeforeChange: () => {
          return new Promise((resolve, reject) => {
            IBestDialogUtil.open({
              title: "提示",
              message: "确定更改状态?",
              showCancelButton: true,
              onConfirm: () => {
                resolve()
              },
              onCancel: () => {
                reject()
              }
            })
          })
        }
      })
    }
  }
}

API

@Props

参数说明类型默认值
value默认是否选中, 支持双向绑定, 1.0.5 开始支持stringnumber类型boolean | string | numberfalse
disabled是否禁用按钮booleanfalse
loading是否显示为加载状态booleanfalse
switchSize大小尺寸string | number26
activeColor打开时的背景色ResourceColor#1989fa
inactiveColor关闭时的背景色ResourceColor#e2e3e7
loadingActiveColor打开时的 loading 颜色,默认跟随 activeColorResourceColor''
loadingInactiveColor关闭时的 loading 颜色,默认跟随 activeColorResourceColor''
componentWidth 1.0.5组件宽度string | number''
componentPadding 1.0.5组件内边距string | number2
activeText 1.0.5打开时的文字内容ResourceStr''
inactiveText 1.0.5关闭时的文字内容ResourceStr''
activeIcon 1.0.5打开时的图标, 优先级高于activeTextResourceStr''
inactiveIcon 1.0.5关闭时的图标, 优先级高于inactiveTextResourceStr''
textFontSize 1.0.5文字/图片大小string | number14
activeTextColor 1.0.5打开时的文字/图标颜色ResourceColor#fff
inactiveTextColor 1.0.5关闭时的文字/图标颜色ResourceColor#fff
activeValue 1.0.5打开时的值boolean | string | numbertrue
inactiveValue 1.0.5关闭时的值boolean | string | numberfalse

Events

事件名说明事件类型
onChange开关状态改变的回调事件(value: boolean) => void
onBeforeChange开关状态改变前的回调事件,value 为将要改变的状态, 返回Promise<false>false, 可阻止按钮状态的变化。(value: boolean) => Promise<boolean> | boolean
onSwitchClick点击开关的回调事() => void

插槽

插槽名说明类型
nodeBuilder自定义按钮的内容CustomBuilder

主题定制

组件提供了下列颜色变量,可用于自定义深色/浅色模式样式,使用方法请参考 颜色模式 章节,如需要其它颜色变量可提 issue

名称描述默认值
ibest_switch_background关闭时的背景色#e2e3e7