From 6c4bcd2073a5f52473d26a3304aa29ac6b3e1fa4 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Wed, 18 Nov 2020 18:06:27 +0100 Subject: [PATCH] fix messageinput --- example/lib/main.dart | 149 ++++++++++++++++++++---------------- example/pubspec.yaml | 2 +- lib/src/message_input.dart | 20 ++--- lib/src/stream_channel.dart | 4 +- pubspec.yaml | 2 +- 5 files changed, 98 insertions(+), 79 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index fa1c14a6..c4917e51 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -6,8 +6,8 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart'; -import 'chips_input_text_field.dart'; +import 'chips_input_text_field.dart'; import 'notifications_service.dart'; void main() async { @@ -422,9 +422,12 @@ class _NewChatScreenState extends State { bool _isSearchActive = false; + Channel channel; + @override void initState() { super.initState(); + channel = StreamChat.of(context).client.channel('messaging'); _controller = TextEditingController() ..addListener(() { setState(() { @@ -452,74 +455,88 @@ class _NewChatScreenState extends State { style: TextStyle(color: Colors.black), ), ), - body: UsersBloc( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - ChipsInputTextField( - key: _chipInputTextFieldStateKey, - controller: _controller, - focusNode: FocusNode(), - chipBuilder: (context, user) { - return InputChip( - key: ObjectKey(user), - label: Text( - user.name, - style: TextStyle(color: Colors.black), - ), - avatar: UserAvatar( - user: user, - ), - onDeleted: () => _chipInputTextFieldState.removeItem(user), - materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, - ); - }, - onChipAdded: (user) { - setState(() => _selectedUsers.add(user)); - }, - onChipRemoved: (user) { - setState(() => _selectedUsers.remove(user)); - }, - ), - Container( - width: double.maxFinite, - color: Colors.white54, - child: Padding( - padding: const EdgeInsets.symmetric( - vertical: 8, - horizontal: 8, - ), - child: Text( - _isSearchActive - ? "Matches for \"$_userNameQuery\"" - : 'On the platform', - style: TextStyle( - fontWeight: FontWeight.w500, - ), - ), - ), - ), - Expanded( - child: UserListView( - filterByUserName: _userNameQuery, - selectedUsers: _selectedUsers, - groupAlphabetically: _isSearchActive ? false : true, - onUserTap: (user, _) { - if (!_selectedUsers.contains(user)) { - _controller.clear(); - _chipInputTextFieldState - ..addItem(user) - ..pauseItemAddition(); - } + body: StreamChannel( + showLoading: false, + channel: channel, + child: UsersBloc( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + ChipsInputTextField( + key: _chipInputTextFieldStateKey, + controller: _controller, + focusNode: FocusNode(), + chipBuilder: (context, user) { + return InputChip( + key: ObjectKey(user), + label: Text( + user.name, + style: TextStyle(color: Colors.black), + ), + avatar: UserAvatar( + user: user, + ), + onDeleted: () => _chipInputTextFieldState.removeItem(user), + materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, + ); }, - pagination: PaginationParams( - limit: 25, + onChipAdded: (user) { + setState(() => _selectedUsers.add(user)); + }, + onChipRemoved: (user) { + setState(() => _selectedUsers.remove(user)); + }, + ), + Container( + width: double.maxFinite, + color: Colors.white54, + child: Padding( + padding: const EdgeInsets.symmetric( + vertical: 8, + horizontal: 8, + ), + child: Text( + _isSearchActive + ? "Matches for \"$_userNameQuery\"" + : 'On the platform', + style: TextStyle( + fontWeight: FontWeight.w500, + ), + ), ), ), - ), - MessageInput( - ), - ], + Expanded( + child: UserListView( + filterByUserName: _userNameQuery, + selectedUsers: _selectedUsers, + groupAlphabetically: _isSearchActive ? false : true, + onUserTap: (user, _) { + if (!_selectedUsers.contains(user)) { + _controller.clear(); + _chipInputTextFieldState + ..addItem(user) + ..pauseItemAddition(); + } + }, + pagination: PaginationParams( + limit: 25, + ), + ), + ), + MessageInput( + preMessageSending: (message) async { + channel.extraData = { + 'members': [ + ..._selectedUsers.map((e) => e.id), + channel.client.state.user.id, + ], + }; + await channel.watch(); + return message; + }, + ), + ], + ), ), ), ); diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 8fc2dadb..fca549d2 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,6 +1,6 @@ name: example description: A new Flutter project. -version: 1.0.59+61 +version: 1.0.60+62 environment: sdk: ">=2.2.2 <3.0.0" diff --git a/lib/src/message_input.dart b/lib/src/message_input.dart index 6024169b..6f89db84 100644 --- a/lib/src/message_input.dart +++ b/lib/src/message_input.dart @@ -364,7 +364,10 @@ class MessageInputState extends State { controller: textEditingController, focusNode: _focusNode, onChanged: (s) { - StreamChannel.of(context).channel.keyStroke(); + StreamChannel.of(context) + .channel + .keyStroke() + .catchError((e) {}); setState(() { _messageIsPresent = s.trim().isNotEmpty; @@ -479,7 +482,9 @@ class MessageInputState extends State { void _checkMentions(String s, BuildContext context) { if (textEditingController.selection.isCollapsed && - (s.isNotEmpty && s[textEditingController.selection.start - 1] == '@' || + (s.isNotEmpty && + textEditingController.selection.start > 0 && + s[textEditingController.selection.start - 1] == '@' || textEditingController.text .substring(0, textEditingController.selection.start) .split(' ') @@ -1569,14 +1574,9 @@ class MessageInputState extends State { child: Padding( padding: const EdgeInsets.all(8.0), child: Center( - child: InkWell( - onTap: () { - sendMessage(); - }, - child: Icon( - _getIdleSendIcon(), - color: Colors.grey, - ), + child: Icon( + _getIdleSendIcon(), + color: Colors.grey, )), ), ); diff --git a/lib/src/stream_channel.dart b/lib/src/stream_channel.dart index ce19b261..6fafa070 100644 --- a/lib/src/stream_channel.dart +++ b/lib/src/stream_channel.dart @@ -12,12 +12,14 @@ class StreamChannel extends StatefulWidget { Key key, @required this.child, @required this.channel, + this.showLoading = true, }) : super( key: key, ); final Widget child; final Channel channel; + final bool showLoading; /// Use this method to get the current [StreamChannelState] instance static StreamChannelState of(BuildContext context) { @@ -155,7 +157,7 @@ class StreamChannelState extends State { future: widget.channel.initialized, initialData: widget.channel.state != null, builder: (context, snapshot) { - if (!snapshot.hasData || !snapshot.data) { + if (widget.showLoading && (!snapshot.hasData || !snapshot.data)) { return Container( height: 30, child: Center( diff --git a/pubspec.yaml b/pubspec.yaml index 9c1b0a29..7e7fe36b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -27,7 +27,7 @@ dependencies: file_picker: ^2.0.12 image_picker: ^0.6.7+2 flutter_keyboard_visibility: ^3.3.0 - stream_chat: ^0.2.13 + stream_chat: ^0.2.13+1 mime: ^0.9.6+3 visibility_detector: ^0.1.5 http_parser: ^3.1.4