讨论广场 问答详情
Button在加上stateStyles时,无法正常使用渐变效果
m0_64711099 2024-07-27 11:09:44
7 评论 分享
harmonyos
@Component 
@Preview 
export default struct PrimaryButton { 
  text: ResourceStr = '' 
 
  build() { 
    Button(this.text) 
      .fontColor($r('app.color.white')) 
      .fontSize($r('app.float.font_16')) 
      .height("45vp") 
      .width("100%") 
      .enabled(true) 
      .stateEffect(true) 
      .backgroundColor($r('app.color.trans')) 
      .stateStyles({ 
        normal: { 
          .backgroundColor($r('app.color.primary_button')) 
          // .linearGradient({ 
          //   direction: GradientDirection.Right, 
          //   colors: [[0x67CF7C, 0], [0x39C9A0, 1]] 
          // }) 
        }, 
        disabled: { 
          .backgroundColor($r('app.color.font_black_ccc')) 
          // .linearGradient({ 
          //   colors: [[0xccc, 0], [0xccc, 1]] 
          // }) 
        } 
      }) 
  } 
}

用上之后,不管是enable还是disable,效果都不正确

7 评论 分享
写回答
全部评论(1)
1 楼

直接设置enable属性,与stateStyles 初始化是不联动的,可以提供传入状态值来实现所需要的逻辑。

@State 
status: boolean = false 
Button("Click Me") 
  .enabled(this.status)

 

2024-07-27 11:10:15