Skip to content

Commit

Permalink
feat: Fixes issue #303,#263: ✨Added send message on enter button.
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhsimformsolutions committed Jan 7, 2025
1 parent b3e0af0 commit 159ef5d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/src/models/config_models/send_message_configuration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class SendMessageConfiguration {
/// Used to give reply dialog color.
final Color? replyDialogColor;

/// Provides call when user add action on submit when enter is pressed.
final Function(String val)? onSubmitted;

/// Used to give color to title of reply pop-up.
final Color? replyTitleColor;

Expand Down Expand Up @@ -87,6 +90,7 @@ class SendMessageConfiguration {
this.replyDialogColor,
this.replyTitleColor,
this.replyMessageColor,
this.onSubmitted,
this.closeIconColor,
this.allowRecordingVoice = true,
this.enableCameraImagePicker = true,
Expand Down Expand Up @@ -137,6 +141,9 @@ class TextFieldConfiguration {
/// Used to give text style of hint text in text field.
final TextStyle? hintStyle;

/// Used to text input action from keyboard enter button.
final TextInputAction? textInputAction;

/// Used to give text style of actual text in text field.
final TextStyle? textStyle;

Expand Down Expand Up @@ -183,6 +190,7 @@ class TextFieldConfiguration {
this.compositionThresholdTime = const Duration(seconds: 1),
this.inputFormatters,
this.textCapitalization,
this.textInputAction = TextInputAction.newline,
this.enabled = true,
});
}
Expand Down
31 changes: 31 additions & 0 deletions lib/src/widgets/chatui_textfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import 'package:audio_waveforms/audio_waveforms.dart';
import 'package:chatview/src/utils/constants/constants.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:image_picker/image_picker.dart';

import '../../chatview.dart';
Expand Down Expand Up @@ -182,6 +183,14 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
keyboardType: textFieldConfig?.textInputType,
inputFormatters: textFieldConfig?.inputFormatters,
onChanged: _onChanged,
onSubmitted: (v) {
if (sendMessageConfig?.onSubmitted != null) {
sendMessageConfig!.onSubmitted!(v);
} else {
_onSubmitted(v);
}
},
textInputAction:textFieldConfig?.textInputAction ?? TextInputAction.newline,
enabled: textFieldConfig?.enabled,
textCapitalization: textFieldConfig?.textCapitalization ??
TextCapitalization.sentences,
Expand Down Expand Up @@ -373,6 +382,28 @@ class _ChatUITextFieldState extends State<ChatUITextField> {
}
}

bool _isWebOrDesktop() {
return kIsWeb || Platform.isMacOS || Platform.isWindows || Platform.isLinux;
}

void _onSubmitted(String inputText){
bool isShiftPressed = HardwareKeyboard.instance.isShiftPressed;
if(_isWebOrDesktop() && isShiftPressed){
widget.textEditingController.text += '\n';
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.textEditingController.selection = TextSelection.fromPosition(
TextPosition(offset: widget.textEditingController.text.length),
);
widget.focusNode.requestFocus();
});
}else{
if(inputText.isNotEmpty){
widget.onPressed();
_inputText.value = '';
}
}
}

void _onChanged(String inputText) {
debouncer.run(() {
composingStatus.value = TypeWriterStatus.typed;
Expand Down

0 comments on commit 159ef5d

Please sign in to comment.