文档开发者中心
我的
|

DevEco Studio

想要优化如下代码,有什么好的实现方式嘛~

目前的代码行数太多,且很多地方都是重复使用的,请问可以怎么优化一下~

* 这里的 screenHeight 是屏幕高度(去掉啦状态栏的)  这里给个默认值2594

收起
自动换行
深色代码主题
复制
@Entry
@Component
struct HollowPage {
  @State handlePopup: boolean = false
  @State num: number = 0
  @State indicatorTypes: string[] = ['KLINE', 'KDJ', 'BOLL']
  @State indicatorTypesMore: string[] = ['SAR', 'K线', 'BOLL', 'SAR', 'K线', 'BOLL', 'SAR', 'K线', 'BOLL', 'SAR', 'K线', 'BOLL',
    'SAR', 'K线', 'BOLL', 'SAR', 'K线', 'BOLL', 'SAR', 'K线', 'BOLL', 'SAR', 'K线', 'BOLL',]
  // 状态栏高度
  @StorageLink("screenHe") screenHeight: number = 2594
  // 记录当前指标选择框距离屏幕底部的距离
  @State targetBottomHeight_1: number = 0
  @State targetBottomHeight_2: number = 0
  @State targetBottomHeight_3: number = 0
  // 设置指标类型列表的高度
  @State targetListHeight_1: number | null = null
  @State targetListHeight_2: number | null = null
  @State targetListHeight_3: number | null = null
  // 控制指标类型的列表是否展示
  @State handlePopup_1: boolean = false
  @State handlePopup_2: boolean = false
  @State handlePopup_3: boolean = false
  // 展示当前指标类型的索引值
  @State targetIndex_1: number = 0
  @State targetIndex_2: number = 0
  @State targetIndex_3: number = 0
  // 展示当前指标类型的索引值
  @State newTargetListHeight_1: number = 0
  @State newTargetListHeight_2: number = 0
  @State newTargetListHeight_3: number = 0

  @Builder
  popupBuilder(indicatorType: string) {
    if (indicatorType == this.indicatorTypes[0]) {
      Scroll() {
        Column() {
          ForEach(this.indicatorTypes, (item: string, index: number) => {
            Column() {
              Text(item).margin({ top: 10, bottom: 10 }).fontColor(this.targetIndex_1 == index ? '#ff5500' : '#000000')
              if (index !== this.indicatorTypes.length - 1) {
                Divider().strokeWidth(0.5).color('#2E000000').margin({ left: 15, right: 15 })
              }
            }.onClick(() => {
              this.handlePopup_1 = !this.handlePopup_1
              this.targetIndex_1 = index
            })
          })
        }.onAreaChange((oldArea: Area, newArea: Area) => {
          this.newTargetListHeight_1 = newArea.height as number
          this.targetListHeight_1 = this.newTargetListHeight_1 > this.targetBottomHeight_1 ? this.targetBottomHeight_1 : this.newTargetListHeight_1
        })
      }.scrollable(ScrollDirection.Vertical).scrollBar(BarState.Off)
      .height(this.targetListHeight_1)
    } else if (indicatorType == this.indicatorTypes[1]) {
      Scroll() {
        Column() {
          ForEach(this.indicatorTypesMore, (item: string, index: number) => {
            Column() {
              Text(item).margin({ top: 10, bottom: 10 }).fontColor(this.targetIndex_2 == index ? '#ff5500' : '#000000')
              if (index !== this.indicatorTypesMore.length - 1) {
                Divider().strokeWidth(0.5).color('#2E000000').margin({ left: 15, right: 15 })
              }
            }.onClick(() => {
              this.handlePopup_2 = !this.handlePopup_2
              this.targetIndex_2 = index
            })
          })
        }.onAreaChange((oldArea: Area, newArea: Area) => {
          this.newTargetListHeight_2 = newArea.height as number
          this.targetListHeight_2 = this.newTargetListHeight_2 > this.targetBottomHeight_2 ? this.targetBottomHeight_2 - 10 : this.newTargetListHeight_2
        })
      }.scrollable(ScrollDirection.Vertical).scrollBar(BarState.Off)
      .height(this.targetListHeight_2)
    } else if (indicatorType == this.indicatorTypes[2]) {
      Scroll() {
        Column() {
          ForEach(this.indicatorTypesMore, (item: string, index: number) => {
            Column() {
              Text(item).margin({ top: 10, bottom: 10 }).fontColor(this.targetIndex_3 == index ? '#ff5500' : '#000000')
              if (index !== this.indicatorTypesMore.length - 1) {
                Divider().strokeWidth(0.5).color('#2E000000').margin({ left: 15, right: 15 })
              }
            }.onClick(() => {
              this.handlePopup_3 = !this.handlePopup_3
              this.targetIndex_3 = index
            })
          })
        }.onAreaChange((oldArea: Area, newArea: Area) => {
          this.newTargetListHeight_3 = newArea.height as number
          this.targetListHeight_3 = this.newTargetListHeight_3 > this.targetBottomHeight_3 ? this.targetBottomHeight_3 - 10 : this.newTargetListHeight_3
        })
      }.scrollable(ScrollDirection.Vertical).scrollBar(BarState.Off)
      .height(this.targetListHeight_3)
    }
  }

  build() {
    Column() {
      ForEach(this.indicatorTypes, (indicatorType: string, index: number) => {


        if (indicatorType == this.indicatorTypes[0]) {
          Text() {
            Span(this.indicatorTypes[this.targetIndex_1])
            ImageSpan($r('app.media.ic_trangle_top')).rotate({ angle: 180 })
              .width(8)
              .verticalAlign(ImageSpanAlignment.CENTER)
              .margin({ left: 5 })
          }
          .fontSize(12)
          .align(Alignment.TopStart)
          .backgroundColor('#F5F5F5')
          .margin({ left: 10, top: 5 })
          .padding({ top: 2, bottom: 2, left: 4, right: 4 })
          .borderRadius(5)
          .onAreaChange((oldArea: Area, newArea: Area) => {
            this.targetBottomHeight_1 = (px2vp(this.screenHeight) - (newArea.height as number) - (newArea.globalPosition.y as number))
          })
          .onClick(() => {
            this.handlePopup_1 = !this.handlePopup_1
          })
          .bindPopup(this.handlePopup_1, {
            builder: this.popupBuilder(indicatorType),
            width: 90,
            radius: 5, //边框圆角
            mask: { color: '#99C0C0C0' }, //遮罩层颜色
            shadow: ShadowStyle.OUTER_DEFAULT_XS, //设置阴影
            placement: Placement.Bottom, //popup组件相对目标的显示位置
          })
        } else if (indicatorType == this.indicatorTypes[1]) {
          Text() {
            Span(this.indicatorTypesMore[this.targetIndex_2])
            ImageSpan($r('app.media.ic_trangle_top')).rotate({ angle: 180 })
              .width(8)
              .verticalAlign(ImageSpanAlignment.CENTER)
              .margin({ left: 5 })
          }
          .fontSize(12)
          .align(Alignment.TopStart)
          .backgroundColor('#F5F5F5')
          .margin({ left: 10, top: 5 })
          .padding({ top: 2, bottom: 2, left: 4, right: 4 })
          .borderRadius(5)
          .onAreaChange((oldArea: Area, newArea: Area) => {
            this.targetBottomHeight_2 = (px2vp(this.screenHeight) - (newArea.height as number) - (newArea.globalPosition.y as number))
          })
          .onClick(() => {
            this.handlePopup_2 = !this.handlePopup_2
          })
          .bindPopup(this.handlePopup_2, {
            builder: this.popupBuilder(indicatorType),
            width: 90,
            radius: 5, //边框圆角
            mask: { color: '#99C0C0C0' }, //遮罩层颜色
            shadow: ShadowStyle.OUTER_DEFAULT_XS, //设置阴影
            placement: Placement.Bottom, //popup组件相对目标的显示位置
          })
        } else if (indicatorType == this.indicatorTypes[2]) {
          Text() {
            Span(this.indicatorTypesMore[this.targetIndex_3])
            ImageSpan($r('app.media.ic_trangle_top')).rotate({ angle: 180 })
              .width(8)
              .verticalAlign(ImageSpanAlignment.CENTER)
              .margin({ left: 5 })
          }
          .fontSize(12)
          .align(Alignment.TopStart)
          .backgroundColor('#F5F5F5')
          .margin({ left: 10, top: 5 })
          .padding({ top: 2, bottom: 2, left: 4, right: 4 })
          .borderRadius(5)
          .onAreaChange((oldArea: Area, newArea: Area) => {
            this.targetBottomHeight_3 = (px2vp(this.screenHeight) - (newArea.height as number) - (newArea.globalPosition.y as number))
          })
          .onClick(() => {
            this.handlePopup_3 = !this.handlePopup_3
          })
          .bindPopup(this.handlePopup_3, {
            builder: this.popupBuilder(indicatorType),
            width: 90,
            radius: 5, //边框圆角
            mask: { color: '#99C0C0C0' }, //遮罩层颜色
            shadow: ShadowStyle.OUTER_DEFAULT_XS, //设置阴影
            placement: Placement.Bottom, //popup组件相对目标的显示位置
          })
        }

        Column() {
          Text(this.targetBottomHeight_1.toString())
          Text(this.targetBottomHeight_2.toString())
          Text(this.targetBottomHeight_3.toString())
        }.width('100%').height(100).backgroundColor('#C0C0C0')

      }, (item: string) => item.toString())
    }.width('100%').height('100%')
  }
}
点赞
1
收藏
回复
5
分享
举报
浏览356 编辑于2024-05-07 06:05辽宁
全部评论
最多点赞
最新发布
最早发布

几个推荐:

1、引用传递要用专门格式:文档示例如下

收起
自动换行
深色代码主题
复制
@Builder function overBuilder($$: { paramA1: string }) {
  Row() {
    Text(`UseStateVarByReference: ${$$.paramA1} `)
  }
}

2、.bindPopup中builder赋值方式:

无参数的@Builder可直接:builer: this.builder_func 不带括号

有参数的@Builder,builder: () => {this.builder(params...);}, 如用引用传递,则:

builder: () => {this.builder({ paramA1: xxx }...);}

3、ForEach循环渲染中数组元素有无更新UI需求,加上元素被作为@Builder的参数,

状态更新UI可能会有挑战,不行则需考虑用@Observed + @ObjectLink, 将字符串数组转换成对象数组,

用对象封装字符串。

组合比较复杂,是否可行只能测试来看。

2楼回复于2024-05-07 16:06 来自浙江

不理解为啥把代码抽出来,点击的时候没有反应!

cke_338.png

3
1楼回复于2024-05-07 06:58 来自辽宁
  • 186******95
    一般来说肯定是指向有问题
    2024-05-07 07:40 来自福建
  • 南 筏回复186******95
    em,俺再试试~builder的点击事件也进不去,所以导致点击没有反应,用引用传参我还没传明白
    2024-05-07 08:04 来自辽宁
  • 186******95回复南 筏
    引用传参写成this.comBtn({a:xxx,b:xxx}),去试一下看看
    2024-05-07 08:11 来自福建
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。