Skip to content

Commit

Permalink
Add shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
demchenkoalex committed Jun 14, 2021
1 parent 459c79c commit 91600f7
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 45 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3

- [WEB] Add shortcuts to send messages on `enter` press

## 1.1.2

- The text inside text messages is now selectable. Thanks @AndreHaueisen for the PR!
Expand Down
130 changes: 86 additions & 44 deletions lib/src/widgets/input.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_chat_types/flutter_chat_types.dart' as types;
import 'attachment_button.dart';
import 'inherited_chat_theme.dart';
import 'inherited_l10n.dart';
import 'send_button.dart';

class NewLineIntent extends Intent {
const NewLineIntent();
}

class SendMessageIntent extends Intent {
const SendMessageIntent();
}

/// A class that represents bottom bar widget with a text field, attachment and
/// send buttons inside. Hides send button when text field is empty.
class Input extends StatefulWidget {
Expand Down Expand Up @@ -88,55 +97,88 @@ class _InputState extends State<Input> {

return GestureDetector(
onTap: () => _inputFocusNode.requestFocus(),
child: Material(
borderRadius: InheritedChatTheme.of(context).theme.inputBorderRadius,
color: InheritedChatTheme.of(context).theme.inputBackgroundColor,
child: Container(
padding: EdgeInsets.fromLTRB(
24 + _query.padding.left,
20,
24 + _query.padding.right,
20 + _query.viewInsets.bottom + _query.padding.bottom,
),
child: Row(
children: [
if (widget.onAttachmentPressed != null) _leftWidget(),
Expanded(
child: TextField(
controller: _textController,
decoration: InputDecoration.collapsed(
hintStyle: InheritedChatTheme.of(context)
.theme
.inputTextStyle
.copyWith(
color: InheritedChatTheme.of(context)
child: Shortcuts(
shortcuts: {
LogicalKeySet(LogicalKeyboardKey.enter): const SendMessageIntent(),
LogicalKeySet(LogicalKeyboardKey.enter, LogicalKeyboardKey.alt):
const NewLineIntent(),
LogicalKeySet(LogicalKeyboardKey.enter, LogicalKeyboardKey.shift):
const NewLineIntent(),
},
child: Actions(
actions: {
SendMessageIntent: CallbackAction<SendMessageIntent>(
onInvoke: (SendMessageIntent intent) => _handleSendPressed(),
),
NewLineIntent: CallbackAction<NewLineIntent>(
onInvoke: (NewLineIntent intent) {
final _newValue = '${_textController.text}\r\n';
_textController.value = TextEditingValue(
text: _newValue,
selection: TextSelection.fromPosition(
TextPosition(offset: _newValue.length),
),
);
},
),
},
child: Focus(
autofocus: true,
child: Material(
borderRadius:
InheritedChatTheme.of(context).theme.inputBorderRadius,
color: InheritedChatTheme.of(context).theme.inputBackgroundColor,
child: Container(
padding: EdgeInsets.fromLTRB(
24 + _query.padding.left,
20,
24 + _query.padding.right,
20 + _query.viewInsets.bottom + _query.padding.bottom,
),
child: Row(
children: [
if (widget.onAttachmentPressed != null) _leftWidget(),
Expanded(
child: TextField(
controller: _textController,
decoration: InputDecoration.collapsed(
hintStyle: InheritedChatTheme.of(context)
.theme
.inputTextColor
.withOpacity(0.5),
.inputTextStyle
.copyWith(
color: InheritedChatTheme.of(context)
.theme
.inputTextColor
.withOpacity(0.5),
),
hintText:
InheritedL10n.of(context).l10n.inputPlaceholder,
),
hintText: InheritedL10n.of(context).l10n.inputPlaceholder,
),
focusNode: _inputFocusNode,
keyboardType: TextInputType.multiline,
maxLines: 5,
minLines: 1,
style: InheritedChatTheme.of(context)
.theme
.inputTextStyle
.copyWith(
color:
InheritedChatTheme.of(context).theme.inputTextColor,
focusNode: _inputFocusNode,
keyboardType: TextInputType.multiline,
maxLines: 5,
minLines: 1,
style: InheritedChatTheme.of(context)
.theme
.inputTextStyle
.copyWith(
color: InheritedChatTheme.of(context)
.theme
.inputTextColor,
),
textCapitalization: TextCapitalization.sentences,
),
textCapitalization: TextCapitalization.sentences,
),
),
Visibility(
visible: _sendButtonVisible,
child: SendButton(
onPressed: _handleSendPressed,
),
Visibility(
visible: _sendButtonVisible,
child: SendButton(
onPressed: _handleSendPressed,
),
),
],
),
),
],
),
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_chat_ui
description: >
Actively maintained, community-driven chat UI implementation with an
optional Firebase BaaS.
version: 1.1.2
version: 1.1.3
homepage: https://flyer.chat
repository: https://github.com/flyerhq/flutter_chat_ui

Expand Down

0 comments on commit 91600f7

Please sign in to comment.