This commit is contained in:
Salvatore Giordano
2021-09-16 16:15:41 +02:00
parent eb3d356407
commit 145685be4b
2 changed files with 67 additions and 55 deletions
@@ -11,7 +11,7 @@ void main() async {
/// from your project dashboard. /// from your project dashboard.
final client = StreamChatClient( final client = StreamChatClient(
's2dxdhpxd94g', 's2dxdhpxd94g',
logLevel: Level.INFO, logLevel: Level.OFF,
); );
/// Set the current user and connect the websocket. In a production /// Set the current user and connect the websocket. In a production
@@ -324,7 +324,9 @@ class MessageInputState extends State<MessageInput> {
late final FocusNode _focusNode; late final FocusNode _focusNode;
bool _inputEnabled = true; bool _inputEnabled = true;
bool _commandEnabled = false; bool _commandEnabled = false;
Widget? _commandsOverlay, _mentionsOverlay, _emojiOverlay; bool _showCommandsOverlay = false;
bool _showEmojiOverlay = false;
bool _showMentionsOverlay = false;
Command? _chosenCommand; Command? _chosenCommand;
bool _actionsShrunk = false; bool _actionsShrunk = false;
@@ -473,26 +475,33 @@ class MessageInputState extends State<MessageInput> {
); );
} }
return AnchoredOverlay( return AnchoredOverlay(
showOverlay: _commandsOverlay != null || showOverlay: _showMentionsOverlay,
_emojiOverlay != null || overlayBuilder: (context, offset) => CenterAbout(
_mentionsOverlay != null,
overlayBuilder: (context, offset) {
late Widget? child;
if (_commandsOverlay != null) {
child = _commandsOverlay;
} else if (_emojiOverlay != null) {
child = _emojiOverlay;
} else {
child = _mentionsOverlay;
}
return CenterAbout(
position: Offset(offset.dx, offset.dy), position: Offset(offset.dx, offset.dy),
child: child!, child: _buildMentionsOverlayEntry(),
); ),
}, child: AnchoredOverlay(
showOverlay: _showCommandsOverlay,
overlayBuilder: (context, offset) => CenterAbout(
position: Offset(offset.dx, offset.dy),
child: _buildCommandsOverlayEntry(),
),
child: AnchoredOverlay(
showOverlay: textEditingController.text.isNotEmpty &&
textEditingController.selection.baseOffset > 0 &&
textEditingController.text
.substring(
0,
textEditingController.selection.baseOffset,
)
.contains(':'),
overlayBuilder: (context, offset) => CenterAbout(
position: Offset(offset.dx, offset.dy),
child: _buildEmojiOverlay(),
),
child: child, child: child,
),
),
); );
} }
@@ -827,10 +836,6 @@ class MessageInputState extends State<MessageInput> {
.keyStroke(widget.parentMessage?.id) .keyStroke(widget.parentMessage?.id)
.catchError((e) {}); .catchError((e) {});
_commandsOverlay = null;
_mentionsOverlay = null;
_emojiOverlay = null;
_checkCommands(s.trim(), context); _checkCommands(s.trim(), context);
_checkMentions(s, context); _checkMentions(s, context);
@@ -879,8 +884,6 @@ class MessageInputState extends State<MessageInput> {
if (textToSelection.endsWith(':') && emoji != null) { if (textToSelection.endsWith(':') && emoji != null) {
_chooseEmoji(splits.sublist(0, splits.length - 1), emoji); _chooseEmoji(splits.sublist(0, splits.length - 1), emoji);
} else {
_emojiOverlay = _buildEmojiOverlay();
} }
} }
} }
@@ -893,7 +896,15 @@ class MessageInputState extends State<MessageInput> {
.split(' ') .split(' ')
.last .last
.contains('@')) { .contains('@')) {
_mentionsOverlay = _buildMentionsOverlayEntry(); if (!_showMentionsOverlay) {
setState(() {
_showMentionsOverlay = true;
});
}
} else if (_showMentionsOverlay) {
setState(() {
_showMentionsOverlay = false;
});
} }
} }
@@ -910,13 +921,22 @@ class MessageInputState extends State<MessageInput> {
if (matchedCommandsList.length == 1) { if (matchedCommandsList.length == 1) {
_chosenCommand = matchedCommandsList[0]; _chosenCommand = matchedCommandsList[0];
textEditingController.clear(); textEditingController.clear();
if (_showCommandsOverlay) {
setState(() { setState(() {
_commandEnabled = true; _commandEnabled = true;
_showCommandsOverlay = false;
}); });
_commandsOverlay = null;
} else {
_commandsOverlay = _buildCommandsOverlayEntry();
} }
} else if (!_showCommandsOverlay) {
setState(() {
_showCommandsOverlay = true;
});
}
} else if (_showCommandsOverlay) {
setState(() {
_showCommandsOverlay = false;
});
} }
} }
@@ -1143,6 +1163,10 @@ class MessageInputState extends State<MessageInput> {
} }
Widget _buildMentionsOverlayEntry() { Widget _buildMentionsOverlayEntry() {
if (textEditingController.value.selection.start < 0) {
return const Offstage();
}
final splits = textEditingController.text final splits = textEditingController.text
.substring(0, textEditingController.value.selection.start) .substring(0, textEditingController.value.selection.start)
.split('@'); .split('@');
@@ -1173,7 +1197,7 @@ class MessageInputState extends State<MessageInput> {
); );
_debounce!.cancel(); _debounce!.cancel();
setState(() { setState(() {
_mentionsOverlay = null; _showMentionsOverlay = false;
}); });
}, },
); );
@@ -1208,8 +1232,6 @@ class MessageInputState extends State<MessageInput> {
offset: rejoin.length, offset: rejoin.length,
), ),
); );
_emojiOverlay = null;
} }
void _setCommand(Command c) { void _setCommand(Command c) {
@@ -1217,8 +1239,8 @@ class MessageInputState extends State<MessageInput> {
setState(() { setState(() {
_chosenCommand = c; _chosenCommand = c;
_commandEnabled = true; _commandEnabled = true;
_showCommandsOverlay = false;
}); });
_commandsOverlay = null;
} }
Widget _buildReplyToMessage() { Widget _buildReplyToMessage() {
@@ -1411,7 +1433,7 @@ class MessageInputState extends State<MessageInput> {
icon: StreamSvgIcon.lightning( icon: StreamSvgIcon.lightning(
color: s.isNotEmpty color: s.isNotEmpty
? _streamChatTheme.colorTheme.disabled ? _streamChatTheme.colorTheme.disabled
: (_commandsOverlay != null : (_showCommandsOverlay
? _messageInputTheme.actionButtonColor ? _messageInputTheme.actionButtonColor
: _messageInputTheme.actionButtonIdleColor), : _messageInputTheme.actionButtonIdleColor),
), ),
@@ -1427,15 +1449,9 @@ class MessageInputState extends State<MessageInput> {
await Future.delayed(const Duration(milliseconds: 300)); await Future.delayed(const Duration(milliseconds: 300));
} }
if (_commandsOverlay == null) {
setState(() { setState(() {
_commandsOverlay = _buildCommandsOverlayEntry(); _showCommandsOverlay = !_showCommandsOverlay;
}); });
} else {
setState(() {
_commandsOverlay = null;
});
}
}, },
); );
@@ -1457,9 +1473,8 @@ class MessageInputState extends State<MessageInput> {
), ),
splashRadius: 24, splashRadius: 24,
onPressed: () async { onPressed: () async {
_emojiOverlay = null; _showCommandsOverlay = false;
_commandsOverlay = null; _showMentionsOverlay = false;
_mentionsOverlay = null;
if (_openFilePickerSection) { if (_openFilePickerSection) {
setState(() => _openFilePickerSection = false); setState(() => _openFilePickerSection = false);
@@ -1745,9 +1760,6 @@ class MessageInputState extends State<MessageInput> {
_commandEnabled = false; _commandEnabled = false;
}); });
_commandsOverlay = null;
_mentionsOverlay = null;
Message message; Message message;
if (widget.editMessage != null) { if (widget.editMessage != null) {
message = widget.editMessage!.copyWith( message = widget.editMessage!.copyWith(