Skip to content

Commit 27ab9a5

Browse files
feat: ✨Added send message on enter button.
1 parent b3e0af0 commit 27ab9a5

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/src/models/config_models/send_message_configuration.dart

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ class SendMessageConfiguration {
4141
/// Used to give reply dialog color.
4242
final Color? replyDialogColor;
4343

44+
/// Provides call when user add action on submit and enter is pressed.
45+
final StringCallback? onSubmitted;
46+
4447
/// Used to give color to title of reply pop-up.
4548
final Color? replyTitleColor;
4649

@@ -87,6 +90,7 @@ class SendMessageConfiguration {
8790
this.replyDialogColor,
8891
this.replyTitleColor,
8992
this.replyMessageColor,
93+
this.onSubmitted,
9094
this.closeIconColor,
9195
this.allowRecordingVoice = true,
9296
this.enableCameraImagePicker = true,
@@ -137,6 +141,9 @@ class TextFieldConfiguration {
137141
/// Used to give text style of hint text in text field.
138142
final TextStyle? hintStyle;
139143

144+
/// Used to text input action from keyboard enter button.
145+
final TextInputAction textInputAction;
146+
140147
/// Used to give text style of actual text in text field.
141148
final TextStyle? textStyle;
142149

@@ -183,6 +190,7 @@ class TextFieldConfiguration {
183190
this.compositionThresholdTime = const Duration(seconds: 1),
184191
this.inputFormatters,
185192
this.textCapitalization,
193+
this.textInputAction = TextInputAction.newline,
186194
this.enabled = true,
187195
});
188196
}

lib/src/widgets/chatui_textfield.dart

+31
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'package:audio_waveforms/audio_waveforms.dart';
2626
import 'package:chatview/src/utils/constants/constants.dart';
2727
import 'package:flutter/foundation.dart';
2828
import 'package:flutter/material.dart';
29+
import 'package:flutter/services.dart';
2930
import 'package:image_picker/image_picker.dart';
3031

3132
import '../../chatview.dart';
@@ -182,6 +183,14 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
182183
keyboardType: textFieldConfig?.textInputType,
183184
inputFormatters: textFieldConfig?.inputFormatters,
184185
onChanged: _onChanged,
186+
onSubmitted: (inputText) {
187+
if (sendMessageConfig?.onSubmitted != null) {
188+
sendMessageConfig!.onSubmitted!(inputText);
189+
} else {
190+
_onSubmitted(inputText);
191+
}
192+
},
193+
textInputAction:textFieldConfig?.textInputAction ?? TextInputAction.newline,
185194
enabled: textFieldConfig?.enabled,
186195
textCapitalization: textFieldConfig?.textCapitalization ??
187196
TextCapitalization.sentences,
@@ -373,6 +382,28 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
373382
}
374383
}
375384

385+
bool _isWebOrDesktop() {
386+
return kIsWeb || Platform.isMacOS || Platform.isWindows || Platform.isLinux;
387+
}
388+
389+
void _onSubmitted(String inputText){
390+
bool isShiftPressed = HardwareKeyboard.instance.isShiftPressed;
391+
if(_isWebOrDesktop() && isShiftPressed){
392+
widget.textEditingController.text += '\n';
393+
WidgetsBinding.instance.addPostFrameCallback((_) {
394+
widget.textEditingController.selection = TextSelection.fromPosition(
395+
TextPosition(offset: widget.textEditingController.text.length),
396+
);
397+
widget.focusNode.requestFocus();
398+
});
399+
}else{
400+
if(inputText.isNotEmpty){
401+
widget.onPressed();
402+
_inputText.value = '';
403+
}
404+
}
405+
}
406+
376407
void _onChanged(String inputText) {
377408
debouncer.run(() {
378409
composingStatus.value = TypeWriterStatus.typed;

0 commit comments

Comments
 (0)