Skip to content

Commit

Permalink
Merge pull request #141 from GetStream/hotfix/input
Browse files Browse the repository at this point in the history
Hotfix/input
  • Loading branch information
deven98 authored Nov 19, 2020
2 parents 1d5f181 + 04da9a0 commit 84fea9f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Flutter action
uses: subosito/flutter-action@v1.3.2
uses: subosito/flutter-action@v1.4.0
with:
channel: 'stable'
- name: Get dependencies
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.13+1

- Use TextEditingController.addListener instead of TextField.onChanged

## 0.2.13

- Update llc dependency
Expand Down
136 changes: 62 additions & 74 deletions lib/src/message_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ enum DefaultAttachmentTypes {
/// Modify it to change the widget appearance.
class MessageInput extends StatefulWidget {
/// Instantiate a new MessageInput
MessageInput(
{Key key,
this.onMessageSent,
this.preMessageSending,
this.parentMessage,
this.editMessage,
this.maxHeight = 150,
this.keyboardType = TextInputType.multiline,
this.disableAttachments = false,
this.doImageUploadRequest,
this.doFileUploadRequest,
this.initialMessage,
this.textEditingController,
this.actions,
this.actionsLocation = ActionsLocation.left,
this.attachmentThumbnailBuilders,
this.inputTextStyle,
this.attachmentIconColor})
: super(key: key);
MessageInput({
Key key,
this.onMessageSent,
this.preMessageSending,
this.parentMessage,
this.editMessage,
this.maxHeight = 150,
this.keyboardType = TextInputType.multiline,
this.disableAttachments = false,
this.doImageUploadRequest,
this.doFileUploadRequest,
this.initialMessage,
this.textEditingController,
this.actions,
this.actionsLocation = ActionsLocation.left,
this.attachmentThumbnailBuilders,
this.inputTextStyle,
this.attachmentIconColor,
}) : super(key: key);

/// Message to edit
final Message editMessage;
Expand Down Expand Up @@ -252,36 +252,6 @@ class MessageInputState extends State<MessageInput> {
keyboardType: widget.keyboardType,
controller: textEditingController,
focusNode: _focusNode,
onChanged: (s) {
StreamChannel.of(context).channel.keyStroke(
widget.parentMessage?.id,
);

setState(() {
_messageIsPresent = s.trim().isNotEmpty;
});

_commandsOverlay?.remove();
_commandsOverlay = null;
_mentionsOverlay?.remove();
_mentionsOverlay = null;

if (s.startsWith('/')) {
_commandsOverlay = _buildCommandsOverlayEntry();
Overlay.of(context).insert(_commandsOverlay);
}

if (textEditingController.selection.isCollapsed &&
(s[textEditingController.selection.start - 1] == '@' ||
textEditingController.text
.substring(0, textEditingController.selection.start)
.split(' ')
.last
.contains('@'))) {
_mentionsOverlay = _buildMentionsOverlayEntry();
Overlay.of(context).insert(_mentionsOverlay);
}
},
onTap: () {
setState(() {
_typingStarted = true;
Expand Down Expand Up @@ -403,7 +373,7 @@ class MessageInputState extends State<MessageInput> {

OverlayEntry _buildMentionsOverlayEntry() {
final splits = textEditingController.text
.substring(0, textEditingController.value.selection.start)
.substring(0, textEditingController.value.selection.baseOffset)
.split('@');
final query = splits.last.toLowerCase();

Expand Down Expand Up @@ -467,7 +437,7 @@ class MessageInputState extends State<MessageInput> {
text: rejoin +
textEditingController.text.substring(
textEditingController
.selection.start),
.selection.baseOffset),
selection: TextSelection.collapsed(
offset: rejoin.length,
),
Expand Down Expand Up @@ -962,41 +932,59 @@ class MessageInputState extends State<MessageInput> {
if (!kIsWeb) {
_keyboardListener = KeyboardVisibility.onChange.listen((visible) {
if (visible) {
if (_commandsOverlay != null) {
if (textEditingController.text.startsWith('/')) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_commandsOverlay = _buildCommandsOverlayEntry();
Overlay.of(context).insert(_commandsOverlay);
});
}
}

if (_mentionsOverlay != null) {
if (textEditingController.text.contains('@')) {
WidgetsBinding.instance.addPostFrameCallback((_) {
_mentionsOverlay = _buildCommandsOverlayEntry();
Overlay.of(context).insert(_mentionsOverlay);
});
}
}
_onChange();
} else {
if (_commandsOverlay != null) {
_commandsOverlay.remove();
}
if (_mentionsOverlay != null) {
_mentionsOverlay.remove();
}
_commandsOverlay?.remove();
_commandsOverlay = null;
_mentionsOverlay?.remove();
_mentionsOverlay = null;
}
});
}

textEditingController =
widget.textEditingController ?? TextEditingController();

textEditingController.addListener(_onChange);

if (widget.editMessage != null || widget.initialMessage != null) {
_parseExistingMessage(widget.editMessage ?? widget.initialMessage);
}
}

void _onChange() {
final s = textEditingController.text;
StreamChannel.of(context).channel.keyStroke(
widget.parentMessage?.id,
);

setState(() {
_messageIsPresent = s.trim().isNotEmpty;
});

_commandsOverlay?.remove();
_commandsOverlay = null;
_mentionsOverlay?.remove();
_mentionsOverlay = null;

if (s.trim().startsWith('/')) {
_commandsOverlay = _buildCommandsOverlayEntry();
Overlay.of(context).insert(_commandsOverlay);
}

if (_messageIsPresent &&
textEditingController.selection.isCollapsed &&
textEditingController.selection.baseOffset > 0 &&
textEditingController.text
.substring(0, textEditingController.selection.baseOffset)
.split(' ')
.last
.contains('@')) {
_mentionsOverlay = _buildMentionsOverlayEntry();
Overlay.of(context).insert(_mentionsOverlay);
}
}

void _parseExistingMessage(Message message) {
textEditingController.text = message.text;

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: stream_chat_flutter
homepage: https://github.com/GetStream/stream-chat-flutter
description: Stream Chat official Flutter SDK. Build your own chat experience using Dart and Flutter.
version: 0.2.13
version: 0.2.13+1
repository: https://github.com/GetStream/stream-chat-flutter
issue_tracker: https://github.com/GetStream/stream-chat-flutter/issues

Expand Down

0 comments on commit 84fea9f

Please sign in to comment.