当前位置:   article > 正文

如何使用ArkTS编写一个HarmonyOS原生聊天UI框架。_使用arkts编写一个聊天软件

使用arkts编写一个聊天软件

简介

ChatUI ,是一个ArkTS编写的HarmonyOS原生聊天UI框架,提供了开箱即用的聊天对话组件。

下载安装

ohpm install @changwei/chatui

OpenHarmony ohpm 环境配置等更多内容。

接口和属性列表

接口列表

接口参数功能
setTyping(isTyping)isTyping:布尔值显示/隐藏消息加载动画
postMessage(msg,clearInput)msg:ChatMessage类型
clearInput: boolean类型。
在对话界面中显示消息
指示展示消息时是否清空输入框内容,默认清除。
submitUserInput(userIputText)userIputText:string类型。触发Chat组件用户发送消息事件
onSendMessage(callback)callback:(ctl,message)=>void用户发送输入消息回调事件
onClear(callback)callback:(event)=>void用户清空聊天记录回调事件

属性列表

属性描述
messages聊天消息列表,IChatDataSource类型。支持懒加载显示的数据源
botAvatarchatbot头像(可选)。Resource类型
userAvatar我的头像(可选)。Resource类型
title标题栏标题。string类型
needTitleBar是否显示标题栏。boolean类型
welcomeMessagechatbot默认欢迎语。string类型
botMessageBackgroundColorchatbot消息的背景颜色。string类型
botMessageTextColorchatbot消息的文本颜色。string类型
userMessageBackgroundColor用户消息的背景颜色。string类型
userMessageTextColor用户消息的文本颜色。string类型
messageFontSize消息文本的字体大小。number类型
needBackButton是否显示顶部返回按钮。点击返回导航上一页。boolean类型
needInputControl是否需要底部输入区域。 boolean类型
InputControl底部输入区域,@BuilderParams类型。该区域可自定义为你自己的布局
controller自定义输入控制器,自定义输入区时必填。[ChatController](chatui/src/main/ets/components/Chat.ets · Codex/ChatUI - Gitee.com)类型
backIcon返回按钮图标。Resource类型
clearChatIcon清楚聊天按钮图标。Resource类型
submitButtonText提交消息按钮文本。string类型
inputTextPlaceHolder输入框提示文本。string类型
inputTextPlaceHolderColor输入框提示文本的颜色。string类型
inputTextColor输入文本的颜色。string类型
needSubmitButton是否显示提交消息按钮。boolean类型
useMarkdown是否渲染markdown消息。boolean类型

使用示例

这里演示简单的调用ChatUI组件
  1. import { Chat, ChatRole, ChatMessage } from '@changwei/chatui'
  2. @Entry
  3. @Component
  4. struct Index {
  5. build() {
  6. Row() {
  7. Column() {
  8. Chat({
  9. title:'demo chatbot',
  10. welcomeMessage: '我是你的测试bot',
  11. onSendMessage: (ctl, message) => {
  12. //发送用户消息
  13. ctl.postMessage(message)
  14. //显示回复等待动画
  15. ctl.setTyping(true)
  16. //3秒后发送chatbot响应消息
  17. setTimeout(() => {
  18. ctl.postMessage(new ChatMessage({
  19. role: ChatRole.Assistant,
  20. content: '这是一条测试回复'
  21. }))
  22. // 图片消息
  23. ctl.postMessage(new ChatMessage({
  24. role:ChatRole.Assistant,
  25. picurl:"https://foruda.gitee.com/avatar/1709712450038093632/8548349_changweizhang_1709712449.png"
  26. }));
  27. }, 3000)
  28. }
  29. })
  30. }
  31. }
  32. .height('100%')
  33. }
  34. }
深度定制聊天UI。替换输入区域为你自己的输入组件,替换头像,文本颜色等。
  1. import { Chat, ChatRole, ChatMessage } from '@changwei/chatui'
  2. import { ChatController } from '@changwei/chatui'
  3. import router from '@ohos.router';
  4. @Entry
  5. @Component
  6. struct CustomInput {
  7. @State userInput: string = ''
  8. @State needBackButton: boolean = false
  9. chatController = new ChatController()
  10. build() {
  11. Row() {
  12. Column() {
  13. Chat({
  14. title: 'demo chatbot',
  15. needTitleBar: true,
  16. welcomeMessage: '我是你的测试bot',
  17. botMessageBackgroundColor: Color.Brown,
  18. botMessageTextColor: Color.White,
  19. userMessageBackgroundColor: Color.Green,
  20. userMessageTextColor: Color.White,
  21. botAvatar:$r('app.media.chat'),
  22. messageFontSize: 20,
  23. userInput: this.userInput,
  24. controller: this.chatController,
  25. needBackButton:this.needBackButton,
  26. onSendMessage: (ctl, message) => {
  27. //发送用户消息
  28. ctl.postMessage(message)
  29. this.userInput = ''
  30. //显示回复等待动画
  31. ctl.setTyping(true)
  32. //3秒后发送chatbot响应消息
  33. setTimeout(() => {
  34. ctl.postMessage(new ChatMessage({role:ChatRole.Assistant, content:'这是一条测试回复'}))
  35. }, 3000)
  36. }
  37. })
  38. {
  39. Row() {
  40. Button() {
  41. Image($r('app.media.app_icon'))
  42. }
  43. .backgroundColor('#')
  44. .height('40')
  45. .width('40')
  46. .margin(5)
  47. TextInput({
  48. text: this.userInput
  49. })
  50. .enterKeyType(EnterKeyType.Send)
  51. .fontColor(Color.White)
  52. .backgroundColor(Color.Blue)
  53. .width('80%')
  54. .onChange((val) => {
  55. this.userInput = val
  56. })
  57. .onSubmit((ss) => {
  58. this.chatController.submitUserInput(this.userInput)
  59. })
  60. }
  61. }
  62. }
  63. }
  64. .height('100%')
  65. }
  66. aboutToAppear() {
  67. const params = router.getParams(); // 获取传递过来的参数对象
  68. if(params) {
  69. this.needBackButton = params['needBackButton']
  70. }
  71. }
  72. }
使用Markdown格式显示消息
 Chat({useMarkdown:true})

markdown消息效果请看上面的demo gif

约束与限制

在下述版本验证通过: DevEco Studio: 4.0.0.600, SDK: API9

贡献代码

使用过程中发现任何问题都可以提Issue 给我 。

开源协议

本项目基于 MIT ,请自由地享受和参与开源。

最后

随着鸿蒙开发越来越火热,我了解到现在有很多小伙伴想入行鸿蒙,但又不知道学习哪些鸿蒙开发技术?所以我给大家整理了一份鸿蒙Next全套VIP学习资料

资料包含了:【OpenHarmony多媒体技术、Stage模型、ArkUI多端部署、分布式应用开发、音频、视频、WebGL、Napi组件、OpenHarmony内核、Harmony南向开发、鸿蒙项目实战】等技术知识点。

有需要的小伙伴自行领取:获取完整版方式请点击→《 鸿蒙Next全套VIP学习资料

1.鸿蒙核心技术学习路线

2.大厂面试必问面试题

3.鸿蒙南向开发技术

 4.鸿蒙APP开发必备

 5.HarmonyOS Next 最新全套视频教程

 6.鸿蒙生态应用开发白皮书V2.0PDF

这份全套完整版的学习资料已经全部打包好,朋友们如果需要可以点击鸿蒙Next全套VIP学习资料:免费领取(安全链接,放心点击

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/813916
推荐阅读
相关标签
  

闽ICP备14008679号