想要优化如下代码,有什么好的实现方式嘛~
目前的代码行数太多,且很多地方都是重复使用的,请问可以怎么优化一下~
* 这里的 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辽宁
全部评论
最多点赞
最新发布
最早发布
写回答
所见即所得编辑器, editor1
工具栏
0/50000
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
- 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
- 如您发布的内容为转载内容,请注明内容来源。
我要提问题
了解社区公约,与您携手共创和谐专业的开发者社区。








































苏公安网备 32011402010933号