diff --git a/lib/views/messageview.dart b/lib/views/messageview.dart index bdba1c9f..258f596e 100644 --- a/lib/views/messageview.dart +++ b/lib/views/messageview.dart @@ -7,6 +7,7 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:cwtch/views/peersettingsview.dart'; import 'package:cwtch/widgets/DropdownContacts.dart'; +import 'package:flutter/services.dart'; import 'package:provider/provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; @@ -102,11 +103,13 @@ class _MessageViewState extends State { } void _sendMessage([String? ignoredParam]) { - ChatMessage cm = new ChatMessage(o: 1, d: ctrlrCompose.value.text); - Provider.of(context, listen: false) - .cwtch - .SendMessage(Provider.of(context, listen: false).profileOnion, Provider.of(context, listen: false).onion, jsonEncode(cm)); - _sendMessageHelper(); + if (ctrlrCompose.value.text.isNotEmpty) { + ChatMessage cm = new ChatMessage(o: 1, d: ctrlrCompose.value.text); + Provider.of(context, listen: false) + .cwtch + .SendMessage(Provider.of(context, listen: false).profileOnion, Provider.of(context, listen: false).onion, jsonEncode(cm)); + _sendMessageHelper(); + } } void _sendInvitation([String? ignoredParam]) { @@ -137,12 +140,18 @@ class _MessageViewState extends State { Expanded( child: Container( decoration: BoxDecoration(border: Border(top: BorderSide(color: Provider.of(context).theme.defaultButtonActiveColor()))), + child: RawKeyboardListener( + focusNode: FocusNode(), + onKey: handleKeyPress, child: TextFormField( key: Key('txtCompose'), controller: ctrlrCompose, - autofocus: !Platform.isAndroid, focusNode: focusNode, - textInputAction: TextInputAction.send, + autofocus: !Platform.isAndroid, + textInputAction: TextInputAction.newline, + keyboardType: TextInputType.multiline, + minLines: 1, + maxLines: null, onFieldSubmitted: _sendMessage, decoration: InputDecoration( enabledBorder: InputBorder.none, @@ -160,13 +169,26 @@ class _MessageViewState extends State { tooltip: AppLocalizations.of(context)!.sendMessage, onPressed: _sendMessage, ), - ))), + )))), ), ], ), ); } + // Send the message if enter is pressed without the shift key... + void handleKeyPress(event) { + var data = event.data as RawKeyEventData; + if (data.logicalKey == LogicalKeyboardKey.enter && !event.isShiftPressed) { + final messageWithoutNewLine = ctrlrCompose.value.text.trimRight(); + ctrlrCompose.value = TextEditingValue( + text: messageWithoutNewLine + ); + _sendMessage(); + + } + } + void placeHolder() => {}; // explicitly passing BuildContext ctx here is important, change at risk to own health