一、前言
@Styles仅仅应用于静态页面的样式复用,stateStyles可以依据组件的内部状态的不同,快速设置不同样式。这就是我们本章要介绍的内容stateStyles(又称为:多态样式)。
说明 多态样式仅支持通用属性
二、概述
stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以下五种状态:
- focused:获焦态。
- normal:正常态。
- pressed:按压态。
- disabled:不可用态。
- selected10+:选中态。
三、使用场景
3.1 基础场景
下面的示例展示了stateStyles最基本的使用场景。Button1处于第一个组件,Button2处于第二个组件。按压时显示为pressed态指定的黑色。使用Tab键走焦,先是Button1获焦并显示为focus态指定的粉色。当Button2获焦的时候,Button2显示为focus态指定的粉色,Button1失焦显示normal态指定的蓝色。
@Entry
@Component
struct StateStylesSample {
build() {
Column() {
Button('Button1')
.stateStyles({
focused: {
.backgroundColor('#ffffeef0')
},
pressed: {
.backgroundColor('#FFA51212')
},
normal: {
.backgroundColor('#ff2787d9')
}
})
.margin(20)
Button('Button2')
.stateStyles({
focused: {
.backgroundColor('#ffffeef0')
},
pressed: {
.backgroundColor('#ff707070')
},
normal: {
.backgroundColor('#ff2787d9')
}
})
}.margin('30%')
}
效果图
3.2 @Styles和stateStyles联合使用
以下示例通过@Styles指定stateStyles的不同状态。
效果图
3.3 在stateStyles里使用常规变量和状态变量
stateStyles可以通过this绑定组件内的常规变量和状态变量。
@Entry
@Component
struct StateStylesSample {
@Styles
normalStyle() {
.backgroundColor(Color.Gray)
}
@Styles
pressedStyle() {
.backgroundColor(Color.Red)
}
@State focusedColor: Color = Color.Red;
normalColor: Color = Color.Green
build() {
Column({ space: 16 }) {
Text('Text1')
.fontSize(30)
.fontColor(Color.White)
//通过@Styles指定stateStyles的不同状态。
.stateStyles({
normal: this.normalStyle,
pressed: this.pressedStyle,
})
Button('clickMe').height(100).width(100)
//stateStyles可以通过this绑定组件内的常规变量和状态变量。
.stateStyles({
normal: {
.backgroundColor(this.normalColor)
},
pressed: {
.backgroundColor(this.focusedColor)
}
})
.onClick(() => {
this.focusedColor = Color.Pink
})
Button('Button1')
.stateStyles({
focused: {
.backgroundColor('#ffffeef0')
},
pressed: {
.backgroundColor('#FFA51212')
},
normal: {
.backgroundColor('#ff2787d9')
}
})
.margin(20)
Button('Button2')
.stateStyles({
focused: {
.backgroundColor('#ffffeef0')
},
pressed: {
.backgroundColor('#ff707070')
},
normal: {
.backgroundColor('#ff2787d9')
}
})
}.margin('30%')
}
}
效果图