sendMessage method

void sendMessage(
  1. String text, {
  2. bool displayMessage = true,
  3. String? session_id = null,
  4. String? language,
})

Implementation

void sendMessage(String text, {bool displayMessage = true, String? session_id = null, String? language}) async {

  String? previousText = ''; // Store previously sent text here, if available

  // Check if the text is null and use the previously stored text
  String textToSend = text ?? previousText;

  if (text.trim().isEmpty) return;

  if (textToSend == null || textToSend.isEmpty) {
    // Handle the case where no text is available
    print('No text available to send.');
    return;
  }
  if (displayMessage) {
    final userMessage = ResponseMessage(
      MessageType.user,
      DateTime.now(),
      text: text,
    );

    setState(() {
      _messages.add(userMessage);
      _isloading = true;  // Set loading to true
    });
    _controller.clear();
   _scrollToBottom();
  }

  final loadingMessage = ResponseMessage(
    MessageType.bot,
    DateTime.now(),
    customWidget: Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          _agentName??'HAIVA',
          style: GoogleFonts.questrial(
            color: ColorTheme.accent,
            fontSize: 10,
            fontWeight: FontWeight.bold,
          ),
        ),
        Text(
          " is processing",
          style: GoogleFonts.questrial(
            color: ColorTheme.primary,
            fontSize: 10,
            fontWeight: FontWeight.bold,
          ),
        ),
        SizedBox(width: 8), // Spacing between text and loader
        Container(
          width: 15,
          height: 10,
          child: SpinKitThreeBounce(
            color: ColorTheme.primary,
            size: 10,
          ),
        ),
      ],
    ),
  );
  setState(() {
    _messages.add(loadingMessage);
  });
  String? finalTextToSend = text ?? _prevMessage;
  try {

       final responseMessage = await ChatService().flowMessage(text, widget.agentId, isMarkdown: true, sessionId: _sessionId, language:_currentLocaleId);

       // Update the state after receiving the response
       setState(() {
         _sessionId = responseMessage.sessionId;
         _messages.removeLast();
         if(responseMessage.statusCode == 200){
           _isOnline = true;
         }


         _messages.add(responseMessage);
         // if (_componentType != 'screen') {
         //   _messages.add(responseMessage); // Add the new response message
         // }// Add the new response message
         _isloading = false;
         // _componentType = responseMessage.haivaMessage?['type'];
         _sampleQuestions = responseMessage.haivaMessage?['showCustomQuestions']?['sampleQuestions'];
       });

       if (responseMessage.haivaMessage!.containsKey('prompt')) {
         _prevMessage = _messages[0].text;
       }

  } catch (e) {
    // Handle error, e.g., log the error or show a message to the user
    print("Error: $e");
    setState(() {
      _isloading = false; // Ensure loading state is reset on error
    });
  }

 _scrollToBottom();
}