三、分类模块,通用组件顶部导航栏Navbar

1.封装通用组件顶部导航栏Navbar

不同效果

@Component
export struct MkNavbar {
  @Prop title: string = ''
  @Prop leftIcon: ResourceStr = $r("app.media.ic_public_left")
  @Prop
  rightIcon: ResourceStr = $r("app.media.ic_public_more")
  @Prop
  showLeftIcon: boolean = true
  @Prop
  showRightIcon: boolean = false
  @Prop
  color: ResourceColor = $r("app.color.black")
  @Prop
  bg: ResourceColor = $r("app.color.white")
  // 点击左侧事件
  onLeftClick: () => void = () => {
  }
  onRightClick: () => void = () => {
  }

  @Builder
  defaultBuilder() {
  }

  // 接收参数是Builder类型
  @BuilderParam
  customTitle: () => void = this.defaultBuilder // 可以给也可以不给 不给的话预览器会有报错(编辑器的bug)

  build() {
    RelativeContainer() {
      if (this.showLeftIcon) {
        Image(this.leftIcon)
          .fillColor(this.color)
          .width(30)
          .aspectRatio(1)
          .alignRules({
            center:{
              anchor:'__container__',
              align:VerticalAlign.Center
            },


          })
          .onClick(()=>{
            this.onLeftClick()
          })
      }
      if(this.showRightIcon){
        Image(this.rightIcon)
          .fillColor(this.color)
        .width(30)
          .aspectRatio(1)
          .alignRules({
            center:{
              anchor:'__container__',
              align:VerticalAlign.Center
            },
            right:{

              anchor:'__container__',
              align:HorizontalAlign.End
            }
          })
          .onClick(() => {
            this.onRightClick()
          })
      }
      if(this.title){
        Text(this.title)
          .fontWeight(600)
        .fontColor(this.color)
          .alignRules({
            center:{
              anchor:'__container__',
              align:VerticalAlign.Center
            },
            middle:{
              anchor:'__container__',
              align:HorizontalAlign.Center
            }
          })

      }
      if (this.customTitle) {
        this.customTitle() // 传入的builder被调用了
      }
    }.padding({
      left: 10,
      right: 10
    })
    .width("100%")
    .height(50)
    .backgroundColor(this.bg)
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值