sendMessage method
Sends a message or reply based on the current state.
Validates the content in inputController, then either:
- Sends a new message if replyingTo is
null - Sends a reply if replyingTo is not
null
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);
}
}