ArkTS编写的HarmonyOS原生聊天UI框架

简介

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

 

下载安装

ohpm install @changwei/chatui

OpenHarmony ohpm 环境配置等更多内容,请参考如何安装 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组件

import { Chat, ChatRole, ChatMessage } from '@changwei/chatui'

@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Chat({
          title:'demo chatbot',
          welcomeMessage: '我是你的测试bot',
          onSendMessage: (ctl, message) => {
            //发送用户消息
            ctl.postMessage(message)
            //显示回复等待动画
            ctl.setTyping(true)
            //3秒后发送chatbot响应消息
            setTimeout(() => {
              ctl.postMessage(new ChatMessage({
                role: ChatRole.Assistant,
                content: '这是一条测试回复'
              }))
              // 图片消息
              ctl.postMessage(new ChatMessage({
                role:ChatRole.Assistant,
                picurl:"https://foruda.gitee.com/avatar/1709712450038093632/8548349_changweizhang_1709712449.png"
              }));
            }, 3000)
          }
        })
      }
}
.height('100%')
}
}

深度定制聊天UI。替换输入区域为你自己的输入组件,替换头像,文本颜色等。

import { Chat, ChatRole, ChatMessage } from '@changwei/chatui'
import { ChatController } from '@changwei/chatui'
import router from '@ohos.router';

@Entry
@Component
struct CustomInput {
  @State userInput: string = ''
  @State needBackButton: boolean = false
  chatController = new ChatController()

  build() {
    Row() {
      Column() {
        Chat({
          title: 'demo chatbot',
          needTitleBar: true,
          welcomeMessage: '我是你的测试bot',
          botMessageBackgroundColor: Color.Brown,
          botMessageTextColor: Color.White,
          userMessageBackgroundColor: Color.Green,
          userMessageTextColor: Color.White,
          botAvatar:$r('app.media.chat'),
          messageFontSize: 20,
          userInput: this.userInput,
          controller: this.chatController,
          needBackButton:this.needBackButton,
          onSendMessage: (ctl, message) => {
            //发送用户消息
            ctl.postMessage(message)
            this.userInput = ''
            //显示回复等待动画
            ctl.setTyping(true)
            //3秒后发送chatbot响应消息
            setTimeout(() => {
              ctl.postMessage(new ChatMessage({role:ChatRole.Assistant, content:'这是一条测试回复'}))
            }, 3000)
          }
        })
        {
          Row() {
            Button() {
              Image($r('app.media.app_icon'))
            }
            .backgroundColor('#')
              .height('40')
              .width('40')
              .margin(5)
            
            TextInput({
              text: this.userInput
            })
              .enterKeyType(EnterKeyType.Send)
              .fontColor(Color.White)
              .backgroundColor(Color.Blue)
              .width('80%')
              .onChange((val) => {
                this.userInput = val
              })
              .onSubmit((ss) => {
                this.chatController.submitUserInput(this.userInput)
              })

          }
        }

      }
    }
    .height('100%')
  }

    aboutToAppear() {
      const params = router.getParams(); // 获取传递过来的参数对象
      if(params) {
        this.needBackButton = params['needBackButton']
      }
    }
}

使用Markdown格式显示消息

 Chat({useMarkdown:true})
阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.1024c.cn/archives/21532,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?