Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Added send message on enter button. #305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 and enter is pressed.
final StringCallback? 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: (inputText) {
if (sendMessageConfig?.onSubmitted != null) {
sendMessageConfig!.onSubmitted!(inputText);
} else {
_onSubmitted(inputText);
}
},
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