stateStyles可以依据组件的内部状态的不同,快速设置不同样式
stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以下四种状态:
- focused:获焦态。
- normal:正常态。
- pressed:按压态。
- disabled:不可用态。
使用示例
@State disable: boolean = false
@State pressedColor: Color = Color.Red
@Styles normalStyle() {
.backgroundColor(Color.Orange)
}
build() {
Column() {
Button("ClickMe")
.stateStyles({
// 正常态,与 @Style联合使用
normal: this.normalStyle,
// 获焦态
focused: {
.backgroundColor(Color.Green)
},
// 按压态,支持通过this绑定变量
pressed: {
.backgroundColor(this.pressedColor)
},
// 不可用态
disabled: {
.backgroundColor(Color.Gray)
}
})
.enabled(!this.disable)
Button("让上面的button不可点")
.onClick(() => {
this.disable = !this.disable
})
}
.height('100%')
.width('100%')
}