Skip to content

Commit d9904bd

Browse files
feat: ✨New line based on cursor position
- Added unspecified as the default text input action type.
1 parent 27ab9a5 commit d9904bd

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lib/src/models/config_models/send_message_configuration.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class TextFieldConfiguration {
190190
this.compositionThresholdTime = const Duration(seconds: 1),
191191
this.inputFormatters,
192192
this.textCapitalization,
193-
this.textInputAction = TextInputAction.newline,
193+
this.textInputAction = TextInputAction.unspecified,
194194
this.enabled = true,
195195
});
196196
}

lib/src/widgets/chatui_textfield.dart

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,15 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
185185
onChanged: _onChanged,
186186
onSubmitted: (inputText) {
187187
if (sendMessageConfig?.onSubmitted != null) {
188+
widget.onPressed();
189+
_inputText.value = '';
188190
sendMessageConfig!.onSubmitted!(inputText);
189191
} else {
190192
_onSubmitted(inputText);
191193
}
192194
},
193-
textInputAction:textFieldConfig?.textInputAction ?? TextInputAction.newline,
195+
textInputAction: textFieldConfig?.textInputAction ??
196+
TextInputAction.unspecified,
194197
enabled: textFieldConfig?.enabled,
195198
textCapitalization: textFieldConfig?.textCapitalization ??
196199
TextCapitalization.sentences,
@@ -386,21 +389,24 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
386389
return kIsWeb || Platform.isMacOS || Platform.isWindows || Platform.isLinux;
387390
}
388391

389-
void _onSubmitted(String inputText){
392+
void _onSubmitted(String inputText) {
390393
bool isShiftPressed = HardwareKeyboard.instance.isShiftPressed;
391-
if(_isWebOrDesktop() && isShiftPressed){
392-
widget.textEditingController.text += '\n';
394+
if (_isWebOrDesktop() && isShiftPressed) {
395+
final controller = widget.textEditingController;
396+
final text = controller.text;
397+
final cursorPosition = controller.selection.baseOffset;
398+
final newText = '${text.substring(0, cursorPosition)}\n${text.substring(cursorPosition)}';
399+
controller.text = newText;
400+
// Move cursor to the correct position after the newline
393401
WidgetsBinding.instance.addPostFrameCallback((_) {
394-
widget.textEditingController.selection = TextSelection.fromPosition(
395-
TextPosition(offset: widget.textEditingController.text.length),
402+
controller.selection = TextSelection.fromPosition(
403+
TextPosition(offset: cursorPosition + 1),
396404
);
397405
widget.focusNode.requestFocus();
398406
});
399-
}else{
400-
if(inputText.isNotEmpty){
401-
widget.onPressed();
402-
_inputText.value = '';
403-
}
407+
} else if (inputText.isNotEmpty) {
408+
widget.onPressed();
409+
_inputText.value = '';
404410
}
405411
}
406412

0 commit comments

Comments
 (0)