HarmonyOS6 - 鸿蒙CustomDialog封装信息提示框

HarmonyOS6 - 鸿蒙CustomDialog封装信息提示框

开发环境为:

开发工具:DevEco Studio 6.0.1 Release

API版本是:API21

本文所有代码都已使用模拟器测试成功!

1. 背景

在开发中,我们经常需要使用信息提示框,如Web端组件库ElementUI中,有如下通知框:

一般都包含:成功,警告,消息,错误等类型的提示消息或通知消息

那么移动端的话,在鸿蒙中,我们接下来也自己封装一个组件,实现上述提示框功能,可以在业务场景中灵活的使用

2. 需求

  1. 信息提示框组件代码可以复用
  2. 可以给用户弹框提示的消息类型支持:成功,警告,消息,错误等

3. 分析

基于以上需求,我的开发思路如下:

  1. 分析需求,确定组件功能和UI设计
  2. 创建组件文件,导入必要模块
  3. 使用@CustomDialog装饰器定义组件类
  4. 定义组件属性和控制器
  5. 实现build方法,构建UI布局
  6. 设置外层容器样式(宽度、背景、圆角)
  7. 条件渲染标题区域(使用if判断)
  8. 构建内容区域(图标+文本)
  9. 根据tipsType动态选择图标和颜色
  10. 配置文本样式(字体、行高、字重)
  11. 设置内边距和对齐方式
  12. 测试四种消息类型的显示效果
  13. 验证条件渲染逻辑(有无标题)
  14. 优化代码结构和可读性

4. 开发

基于以上编码思路,弹框组件代码如下:

js 复制代码
/**
 * Author: 波波老师(weixin:javabobo0513)
 * Desc: 通用信息弹框
 */
@CustomDialog
export struct DialogTips {
  controller: CustomDialogController
  dialogTitle: string = '' //弹框标题文本
  dialogContent: string = '' //弹框内容文本
  /**
   * 消息类型(不同类型提示图标是不一样的)
   * 1:成功消息
   * 2:错误消息
   * 3:警告消息
   * 4:普通信息
   */
  tipsType: number = 1

  build() {
    Column() {
      if (this.dialogTitle) {
        Row() {
          //标题
          Text(this.dialogTitle)
            .fontSize(20)
            .fontWeight(700)
        }
        .padding({ left: 20, top: 20, bottom: 12 })
        .justifyContent(FlexAlign.Start)
        .width('100%')
      }

      if (this.dialogContent) {
        //内容
        Column({ space: 12 }) {
          Image(this.tipsType == 1 ? $r('app.media.svg_success') : (this.tipsType == 2 ? $r('app.media.svg_error') :
            (this.tipsType == 3 ? $r('app.media.svg_warn') : $r('app.media.svg_info'))))
            .fillColor(this.tipsType == 1 ? '#40c873' :
              (this.tipsType == 2 ? '#f56c6c' : (this.tipsType == 3 ? '#e6a23c' : '#909399')))
            .width(42)
          Text(this.dialogContent)
            .lineHeight(26)
            .fontSize(18)
            .fontWeight(300)
        }
        .padding(20)
        .width('100%')
        .justifyContent(FlexAlign.Center)
      }
    }
    .width('70%')
    .backgroundColor('#ffffffff')
    .borderRadius(8)
  }
}

使用上述组件的页面测试代码如下:

js 复制代码
import { DialogTips } from '../../component/DialogTips'

@Entry
@Component
struct Page016 {
  @State message: string = ''; //提示信息内容
  @State tipsType: number = 1; //消息类型(1:成功消息;2:错误消息;3:警告消息;4:普通消息)
  //消息提示弹框-声明
  dialogTips: CustomDialogController = new CustomDialogController({
    builder: DialogTips({
      dialogTitle: '', //标题(为空则不显示)
      dialogContent: this.message, //提示信息内容
      tipsType: this.tipsType, //消息类型(1:成功消息;2:错误消息;3:警告消息;4:普通消息)
    }),
    customStyle: true, //开启自定义样式
    alignment: DialogAlignment.Center, // 可设置dialog的对齐方式,设定显示在底部或中间等,默认为底部显示
  })

  build() {
    Column({ space: 10 }) {
      Button('成功')
        .borderRadius(3)
        .width('40%')
        .onClick(() => {
          this.message = '这是一条成功消息!'
          this.tipsType = 1
          this.dialogTips.open();
        })
      Button('错误')
        .borderRadius(3)
        .width('40%')
        .onClick(() => {
          this.message = '这是一条错误消息!'
          this.tipsType = 2
          this.dialogTips.open();
        })
      Button('警告')
        .borderRadius(3)
        .width('40%')
        .onClick(() => {
          this.message = '这是一条警告消息!'
          this.tipsType = 3
          this.dialogTips.open();
        })
      Button('普通')
        .borderRadius(3)
        .width('40%')
        .onClick(() => {
          this.message = '这是一条普通消息!'
          this.tipsType = 4
          this.dialogTips.open();
        })
    }
    .width('100%')
    .height('100%')
  }
}

4. 演示

这样以后我们就可以在不同的页面,使用自己封装消息提示组件进行消息弹框通知了

最后

  • 希望本文对你有所帮助!
  • 本人如有任何错误或不当之处,请留言指出,谢谢!
相关推荐
贾伟康7 小时前
HarmonyOS API 26 Navigation路由栈上限:stackSizeLimit、过载保护与容量观测
harmonyos·arkts·navigation·navpathstack·api26
贾伟康15 小时前
HarmonyOS API 26 Navigation 自适应模式:mode、分栏切换与窗口断点治理
harmonyos·arkts·arkui·navigation·自适应布局
贾伟康15 小时前
【转盘决定吧|07】HarmonyOS ArkTS 6.0.2.130 趣味题库:场景分类、随机选择与详情页解耦
harmonyos·arkts·路由·数据建模·arkui
贾伟康15 小时前
【电路实验模拟|03】HarmonyOS ArkTS 交通灯实战:用定时状态切换红黄绿灯
harmonyos·arkts·canvas·arkui·交通灯
贾伟康1 天前
HarmonyOS API 26 嵌套 Navigation:getParent、父子栈边界与返回分发
harmonyos·arkts·navigation·嵌套路由·navpathstack
贾伟康2 天前
【解压时刻|13】HarmonyOS ArkTS 启动链路审计:从 EntryAbility、AppStore 到 Navigation 首屏
harmonyos·arkts·appstorage·应用启动·entryability
贾伟康2 天前
【古诗词闯关|12】HarmonyOS ArkTS 单页组件拆分实战:降低题库、闯关与结果弹层耦合
harmonyos·arkts·状态管理·arkui·组件化
todoitbo2 天前
把 K-Lite「什么都能播」搬上鸿蒙 PC:影匣播放器的 FFmpeg + libass 原生移植实录
华为·ffmpeg·harmonyos·鸿蒙
c2892480843s3 天前
【鸿蒙心迹】深色模式适配实战:资源限定词、动态取色与一张图两个主题
移动开发·harmonyos·鸿蒙·arkui·深色模式
贾伟康3 天前
HarmonyOS API 26 Navigation 连续栈操作:中间过程合并、最终状态与强制新实例
harmonyos·arkts·navigation·navpathstack·路由治理