import 'package:cached_network_image/cached_network_image.dart'; import 'package:comunic/models/conversation_message.dart'; import 'package:comunic/models/user.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart'; import 'package:comunic/ui/widgets/text_rich_content_widget.dart'; import 'package:comunic/utils/account_utils.dart'; import 'package:comunic/utils/date_utils.dart'; import 'package:comunic/utils/ui_utils.dart'; import 'package:flutter/material.dart'; /// Conversation message tile /// /// @author Pierre HUBERT class ConversationMessageTile extends StatelessWidget { final ConversationMessage message; final User userInfo; final bool isLastMessage; final bool isFirstMessage; const ConversationMessageTile( {Key key, @required this.message, @required this.userInfo, @required this.isLastMessage, @required this.isFirstMessage}) : assert(message != null), assert(userInfo != null), assert(isLastMessage != null), assert(isFirstMessage != null), super(key: key); /// Build account image Widget _buildAccountImage() { return Container( margin: EdgeInsets.all(10.0), child: AccountImageWidget( user: userInfo, width: 35.0, ), ); } /// Build widget image Widget _buildMessageImage(BuildContext context) { return Container( margin: EdgeInsets.only(bottom: 2), child: Material( child: InkWell( onTap: () { showImageFullScreen(context, message.imageURL); }, child: CachedNetworkImage( imageUrl: message.imageURL, width: 200.0, height: 200.0, fit: BoxFit.cover, placeholder: (c, s) => Container( width: 200, height: 200, color: Colors.lightBlueAccent, child: Center( child: CircularProgressIndicator(), ), ), errorWidget: (c, s, o) => Container( width: 200, height: 200, color: Colors.red, child: Center( child: Icon( Icons.error, color: Colors.white, )), ), ), ), borderRadius: BorderRadius.all(Radius.circular(8.0)), clipBehavior: Clip.hardEdge, ), ); } /// Build message date Widget _buildMessageDate() { return isLastMessage ? Container( margin: EdgeInsets.only(top: 5.0), child: Text( dateTimeToString(message.date), style: TextStyle(color: Colors.black54, fontSize: 12.0), textAlign: TextAlign.center, ), ) : Container(); } /// Build a message of the current user Widget _buildRightMessage(BuildContext context) { return Container( margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 10.0), child: Row( mainAxisAlignment: MainAxisAlignment.end, children: [ Column( children: [ Row( children: [ Column( children: [ // Text image Container( child: message.hasImage ? _buildMessageImage(context) : null, ), // Text message Container( child: message.hasMessage ? Container( child: TextRichContentWidget( message.message, textAlign: TextAlign.justify, style: TextStyle(color: Colors.white), ), padding: EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0), width: 200.0, decoration: BoxDecoration( color: Colors.blueAccent, borderRadius: BorderRadius.circular(8.0), ), ) : null, ), ], ), // Account image _buildAccountImage() ], ), // Date Container( child: _buildMessageDate(), margin: EdgeInsets.only(right: 45.0), ) ], ), ], ), ); // Text } /// Build a message of a peer user Widget _buildLeftMessage(BuildContext context) { return Container( margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 5.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ //User name Container( margin: EdgeInsets.only(left: 55.0), child: isFirstMessage ? Text( userInfo.fullName, textAlign: TextAlign.left, style: TextStyle(fontSize: 12.0), ) : null, ), Row( children: [ // Account image _buildAccountImage(), Column( children: [ // Text image Container( child: message.hasImage ? _buildMessageImage(context) : null, ), // Text message Container( child: message.hasMessage ? Container( child: TextRichContentWidget( message.message, textAlign: TextAlign.justify, style: TextStyle(color: Colors.black), ), padding: EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0), width: 200.0, decoration: BoxDecoration( color: Colors.black12, borderRadius: BorderRadius.circular(8.0), ), ) : null, ), ], ), ], ), // Date Container( margin: EdgeInsets.only(left: 50.0), child: Align( alignment: Alignment.topLeft, child: _buildMessageDate(), ), ) ], ), ); } @override Widget build(BuildContext context) { return userID() == message.userID ? _buildRightMessage(context) : _buildLeftMessage(context); } }