sendMessage method

Future<void> sendMessage(
  1. CdxChatLocalizations loc, {
  2. required void onInputError(
    1. String
    ),
})

Sends a message or reply based on the current state.

Validates the content in inputController, then either:

After sending, clears the input field and resets replyingTo if it was set.

loc is the CdxChatLocalizations instance for error messages. onInputError is a callback that will be called with an error message if validation fails.

If the input is empty, this method returns without doing anything.

Implementation

Future<void> sendMessage(
  CdxChatLocalizations loc, {
  required void Function(String) onInputError,
}) async {
  final text = inputController.text.trim();
  if (text.isEmpty) {
    onInputError(loc.message_empty);
    return;
  }

  // Validate message length
  if (text.length > config.maxMessageLength) {
    onInputError(loc.message_too_long(config.maxMessageLength));
    return;
  }

  // Validate number of lines
  final lines = text.split('\n');
  if (lines.length > config.maxLines) {
    onInputError(loc.message_too_many_lines(config.maxLines));
    return;
  }

  try {
    await controller.sendMessage(text);
    inputController.clear();
    controller.setReplyingTo(null);
    notifyListeners();
  } catch (e) {
    onInputError(loc.error_sending_message);
  }
}