曹旭辉 发表于 2023-10-11 15:23:41

【中秋国庆不断更】OpenHarmony多态样式stateStyles使用场景

@Styles和@Extend仅仅应用于静态页面的样式复用,stateStyles可以依据组件的内部状态的不同,快速设置不同样式。这就是我们本章要介绍的内容stateStyles(又称为:多态样式)。
概述

stateStyles是属性方法,可以根据UI内部状态来设置样式,类似于css伪类,但语法不同。ArkUI提供以下四种状态:
​                ● focused:获焦态。
​                ● normal:正常态。
​                ● pressed:按压态。
​                ● disabled:不可用态。
​                ● selected10+:选中态。
使用场景

基础场景

下面的示例展示了stateStyles最基本的使用场景。Button处于第一个组件,默认获焦,生效focused指定的粉色样式。按压时显示为pressed态指定的黑色。如果在Button前再放一个组件,使其不处于获焦态,就会生效normal态的黄色。
@Entry
@Component
struct StateStylesSample {
build() {
    Column() {
      Button('Click me')
      .stateStyles({
          focused: {
            .backgroundColor(Color.Pink)
          },
          pressed: {
            .backgroundColor(Color.Black)
          },
          normal: {
            .backgroundColor(Color.Yellow)
          }
      })
    }.margin('30%')
}
}图1 获焦态和按压态
https://img2023.cnblogs.com/other/2824423/202310/2824423-20231001192848517-1460262150.gif
@Styles和stateStyles联合使用

以下示例通过@Styles指定stateStyles的不同状态。
@Entry
@Component
struct MyComponent {
@Styles normalStyle() {
    .backgroundColor(Color.Gray)
}

@Styles pressedStyle() {
    .backgroundColor(Color.Red)
}

build() {
    Column() {
      Text('Text1')
      .fontSize(50)
      .fontColor(Color.White)
      .stateStyles({
          normal: this.normalStyle,
          pressed: this.pressedStyle,
      })
    }
}
}图2 正常态和按压态
https://img2023.cnblogs.com/other/2824423/202310/2824423-20231001192849195-853295847.gif
在stateStyles里使用常规变量和状态变量

stateStyles可以通过this绑定组件内的常规变量和状态变量。
@Entry
@Component
struct CompWithInlineStateStyles {
@State focusedColor: Color = Color.Red;
normalColor: Color = Color.Green

build() {
    Button('clickMe').height(100).width(100)
      .stateStyles({
      normal: {
          .backgroundColor(this.normalColor)
      },
      focused: {
          .backgroundColor(this.focusedColor)
      }
      })
      .onClick(() => {
      this.focusedColor = Color.Pink
      })
      .margin('30%')
}
}Button默认获焦显示红色,点击事件触发后,获焦态变为粉色。
图3 点击改变获焦态样式
https://img2023.cnblogs.com/other/2824423/202310/2824423-20231001192850315-1064444527.gif
本文由博客一文多发平台 OpenWrite 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 【中秋国庆不断更】OpenHarmony多态样式stateStyles使用场景