From 05f7396c9852e1464dbe3e7a4b6ea01ab02f7aaf Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 25 Apr 2019 20:22:53 +0200 Subject: [PATCH] Improved send message form UI --- lib/ui/screens/conversation_screen.dart | 51 +++++++++++++++---------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/lib/ui/screens/conversation_screen.dart b/lib/ui/screens/conversation_screen.dart index 0107063..b98c97a 100644 --- a/lib/ui/screens/conversation_screen.dart +++ b/lib/ui/screens/conversation_screen.dart @@ -97,21 +97,24 @@ class _ConversationScreenState extends State { image: image, ), ); + + // In case a message was already written in the input + _updatedText(_textEditingController.text); } /// Send a new text message Future _submitTextMessage(BuildContext context, String content) async { - _submitMessage( - context, - NewConversationMessage( - conversationID: widget.conversationID, - message: content, - ), - ); + if (await _submitMessage( + context, + NewConversationMessage( + conversationID: widget.conversationID, + message: content, + )) == + SendMessageResult.SUCCESS) _clearSendMessageForm(); } /// Submit a new message - Future _submitMessage( + Future _submitMessage( BuildContext context, NewConversationMessage message) async { //Send the message _setSending(true); @@ -119,9 +122,7 @@ class _ConversationScreenState extends State { _setSending(false); //Check the result of the operation - if (result == SendMessageResult.SUCCESS) - _clearSendMessageForm(); - else + if (result != SendMessageResult.SUCCESS) Scaffold.of(context).showSnackBar( SnackBar( content: Text( @@ -132,6 +133,14 @@ class _ConversationScreenState extends State { duration: Duration(milliseconds: 500), ), ); + + return result; + } + + void _updatedText(String text) { + setState(() { + _isMessageValid = text.length > 4; + }); } /// Clear send message form @@ -171,22 +180,22 @@ class _ConversationScreenState extends State { new Container( margin: new EdgeInsets.symmetric(horizontal: 4.0), child: new IconButton( - icon: new Icon( - Icons.photo_camera, - color: Theme.of(context).accentColor, - ), - onPressed: () => _sendImage(context)), + icon: new Icon( + Icons.photo_camera, + color: _isSendingMessage + ? Theme.of(context).disabledColor + : Theme.of(context).accentColor, + ), + onPressed: () => _sendImage(context), + ), ), // Message area new Flexible( child: new TextField( + enabled: !_isSendingMessage, controller: _textEditingController, - onChanged: (String messageText) { - setState(() { - _isMessageValid = messageText.length > 4; - }); - }, + onChanged: _updatedText, onSubmitted: _isMessageValid ? (s) => _submitTextMessage(context, s) : null,