sendMessage method

void sendMessage(
  1. String text
)

Implementation

void sendMessage(String text) async {
  if (text.trim().isEmpty) return;

  final userMessage = ResponseMessage(
    MessageType.user,
    DateTime.now(),
    text: text,
  );

  setState(() {
    _messages.add(userMessage);
    _isloading = true;
  });

  _controller.clear();
  _scrollToBottom();


  final loadingMessage = ResponseMessage(
    MessageType.bot,
    DateTime.now(),
    customWidget: Container(
      width: 40,
      child: SpinKitThreeBounce(
        color: ColorTheme.accent,
        size: 15,
      ),
    ),
  );

  setState(() {
    _messages.add(loadingMessage);
  });

  final responseMessage = await ChatService().agentMessage(
    text,
    session_id,
    widget.agentId,
    isMarkdown: true,
    threadId: _threadId,
  );

  setState(() {
    _threadId = responseMessage.threadId;
    _messages.removeLast(); // Remove the loading message
    _messages.add(responseMessage);
    _isloading = false;

  });

  _scrollToBottom();

}