08 HarmonyOS NEXT 仿uv-ui Tag组件开发教程系列(二)

温馨提示:本篇博客的详细代码已发布到 git : https://gitcode.com/nutpi/HarmonyosNext 可以下载运行哦!

目录

    • [Tag 组件进阶特性](#Tag 组件进阶特性)
      • [1. 状态管理](#1. 状态管理)
        • [1.1 组件状态概览](#1.1 组件状态概览)
        • [1.2 内部状态管理](#1.2 内部状态管理)
      • [2. 事件处理机制](#2. 事件处理机制)
        • [2.1 事件类型](#2.1 事件类型)
        • [2.2 事件处理示例](#2.2 事件处理示例)
      • [3. 高级特性](#3. 高级特性)
        • [3.1 动态样式切换](#3.1 动态样式切换)
        • [3.2 自定义样式扩展](#3.2 自定义样式扩展)
      • [4. 进阶应用场景](#4. 进阶应用场景)
        • [4.1 动态标签组](#4.1 动态标签组)
        • [4.2 可选择标签组](#4.2 可选择标签组)
      • [5. 性能优化](#5. 性能优化)

Tag 组件进阶特性

1. 状态管理

1.1 组件状态概览
1.2 内部状态管理
typescript 复制代码
@Component
export struct Tag {
    // 可见性状态
    @State private isVisible: boolean = true
    // 点击状态
    @State private isClicked: boolean = false

    // 状态相关的样式处理
    build() {
        Row() {
            // 组件内容
        }
        .opacity(this.disabled ? 0.5 : 1)
        .backgroundColor(this.getBackgroundColor())
        .onClick(() => {
            if (!this.disabled) {
                this.isClicked = !this.isClicked
            }
        })
    }
}

2. 事件处理机制

2.1 事件类型
2.2 事件处理示例
typescript 复制代码
// 关闭事件处理
if (this.closable) {
    Image($r('app.media.close'))
        .width(16)
        .height(16)
        .margin({ left: 4 })
        .onClick(() => {
            // 内部状态更新
            this.isVisible = false
            // 触发外部回调
            this.onCloseTag && this.onCloseTag()
        })
}

// 点击状态处理
.onClick(() => {
    if (!this.disabled) {
        this.isClicked = !this.isClicked
        // 更新背景色
        this.getBackgroundColor()
    }
})

3. 高级特性

3.1 动态样式切换
typescript 复制代码
private getBackgroundColor(): string {
    // 根据点击状态切换背景色
    if (this.isClicked) {
        const colors: ColorsInterface = {
            default: '#f5f5f5',
            primary: '#e6e9f8',
            success: '#e0f0e0',
            warning: '#f8f0e0',
            danger: '#f8e6e6'
        }
        return Reflect.get(colors, this.type) || colors.default
    }

    // 默认背景色
    const colors: ColorsInterface = {
        default: '#ffffff',
        primary: '#eef2ff',
        success: '#e8f5e9',
        warning: '#fff7e6',
        danger: '#ffebee'
    }
    return Reflect.get(colors, this.type) || colors.default
}
3.2 自定义样式扩展
typescript 复制代码
// 自定义样式接口
interface CustomStyle {
    textColor?: string
    backgroundColor?: string
    borderColor?: string
    // 更多自定义样式属性
}

// 使用示例
Tag({
    text: '自定义样式标签',
    customStyle: {
        textColor: '#8B5CF6',
        backgroundColor: '#EDE9FE',
        borderColor: '#C4B5FD'
    }
})

4. 进阶应用场景

4.1 动态标签组
typescript 复制代码
@State tagList: Array<{
    id: string,
    text: string,
    type: string
}> = [
    { id: '1', text: '标签1', type: 'primary' },
    { id: '2', text: '标签2', type: 'success' }
]

build() {
    Flex({ wrap: FlexWrap.Wrap }) {
        ForEach(this.tagList, (item) => {
            Tag({
                text: item.text,
                type: item.type,
                closable: true,
                onCloseTag: () => {
                    // 从列表中移除标签
                    this.tagList = this.tagList
                        .filter(tag => tag.id !== item.id)
                }
            })
        })
    }
}
4.2 可选择标签组
typescript 复制代码
@State selectedTags: Set<string> = new Set()

build() {
    Flex({ wrap: FlexWrap.Wrap }) {
        ForEach(this.tagList, (item) => {
            Tag({
                text: item.text,
                type: this.selectedTags.has(item.id) ? 'primary' : 'default',
                onClick: () => {
                    if (this.selectedTags.has(item.id)) {
                        this.selectedTags.delete(item.id)
                    } else {
                        this.selectedTags.add(item.id)
                    }
                }
            })
        })
    }
}

5. 性能优化

在优化 HarmonyOS 应用性能时,应当关注状态管理、事件处理和渲染方面的优化。首先,在状态管理方面,应避免不必要的状态更新,确保使用适当的状态作用域,并且合理利用@State 和@Prop 装饰器来管理状态。其次,对于事件处理,应用防抖技术来处理频繁点击,避免在事件处理中进行复杂的计算,同时合理运用事件委托来提高效率。最后,在渲染方面,通过条件渲染减少不必要的 DOM 操作,优化样式计算和切换逻辑,以及合理使用缓存机制,从而提升应用的渲染性能。

下一篇教程将介绍 Tag 组件的实战应用和最佳实践,敬请期待!

相关推荐
李洋-蛟龙腾飞公司5 天前
鸿蒙应用元服务开发-Account Kit配置登录权限
服务器·网络·harmonyos next
枫叶丹48 天前
【HarmonyOS Next之旅】DevEco Studio使用指南(十三) -> ArkTS/TS代码重构
华为·harmonyos·deveco studio·harmonyos next
枫叶丹411 天前
【HarmonyOS Next之旅】DevEco Studio使用指南(十二)
华为·harmonyos·deveco studio·harmonyos next
枫叶丹414 天前
【HarmonyOS Next之旅】DevEco Studio使用指南(十一)
华为·harmonyos·deveco studio·harmonyos next
CV工程师丁Sir1 个月前
《HarmonyOS Next状态栏动画实现案例与代码解析》
华为·harmonyos·harmonyos next
CV工程师丁Sir1 个月前
《HarmonyOS Next视频横竖屏切换及进度条热区拖动实现》
华为·音视频·harmonyos·harmonyos next
CV工程师丁Sir1 个月前
《HarmonyOS Next自定义TabBar页签凸起和凹陷案例与代码》
harmonyos next
枫叶丹41 个月前
【HarmonyOS Next之旅】基于ArkTS开发(三) -> 兼容JS的类Web开发(七) -> JS动画(二)
开发语言·前端·javascript·华为·harmonyos next
CV工程师丁Sir1 个月前
《HarmonyOS Next AI图片文字智能识别与处理实践》
人工智能·harmonyos·easyui·harmonyos next