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. 演示

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

最后

  • 希望本文对你有所帮助!
  • 本人如有任何错误或不当之处,请留言指出,谢谢!
相关推荐
AI_零食3 小时前
鸿蒙的flutter框架表达:生命律动系统
学习·flutter·ui·华为·harmonyos·鸿蒙
AI_零食3 小时前
鸿蒙跨端框架 Flutter 学习 Day 6:Future 在 UI 渲染中的心跳逻辑
学习·flutter·ui·华为·harmonyos·鸿蒙
lbb 小魔仙4 小时前
【Harmonyos】开源鸿蒙跨平台训练营DAY2:多终端工程创建运行、代码提交至AtomGit平台自建公开仓库全流程(附带出现问题及解决方法)
windows·flutter·开源·harmonyos·鸿蒙·开源鸿蒙·鸿蒙开平台应用
AI_零食4 小时前
鸿蒙跨端框架Flutter学习day 2、常用UI组件-弹性布局进阶之道
学习·flutter·ui·华为·harmonyos·鸿蒙
小学生波波5 小时前
HarmonyOS - 鸿蒙开发百度地图案例
地图·百度地图·路线规划·鸿蒙开发·harmonyos6·鸿蒙地图·打点
小学生波波6 小时前
HarmonyOS6 - 鸿蒙电商页面实战案例
登录页面·arkts·鸿蒙系统·电商·harmonyos6
彭不懂赶紧问6 小时前
鸿蒙NEXT开发浅进阶到精通16:从零调试鸿蒙内置AI类API文字转语音场景
华为·harmonyos·鸿蒙·文字转语音
奔跑的露西ly19 小时前
【HarmonyOS NEXT】ArkUI实现「单格单字符+下划线」手机号/验证码输入框
ui·华为·harmonyos·鸿蒙
小白阿龙1 天前
鸿蒙+flutter 跨平台开发——图像编解码与水印嵌入技术实战
flutter·华为·harmonyos·鸿蒙