1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-30 00:36:28 +00:00
comunicmobile/lib/ui/tiles/conversation_message_tile.dart

232 lines
6.9 KiB
Dart
Raw Normal View History

2019-04-26 09:04:06 +00:00
import 'package:cached_network_image/cached_network_image.dart';
2019-04-25 06:56:16 +00:00
import 'package:comunic/models/conversation_message.dart';
import 'package:comunic/models/user.dart';
import 'package:comunic/ui/widgets/account_image_widget.dart';
2019-04-26 06:58:18 +00:00
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/date_utils.dart';
2019-04-26 09:34:24 +00:00
import 'package:comunic/utils/ui_utils.dart';
2019-04-25 06:56:16 +00:00
import 'package:flutter/material.dart';
/// Conversation message tile
///
/// @author Pierre HUBERT
class ConversationMessageTile extends StatelessWidget {
final ConversationMessage message;
final User userInfo;
2019-04-26 06:58:18 +00:00
final bool isLastMessage;
2019-04-26 09:04:06 +00:00
final bool isFirstMessage;
2019-04-25 06:56:16 +00:00
2019-04-26 06:58:18 +00:00
const ConversationMessageTile(
{Key key,
@required this.message,
@required this.userInfo,
2019-04-26 09:04:06 +00:00
@required this.isLastMessage,
@required this.isFirstMessage})
2019-04-25 06:56:16 +00:00
: assert(message != null),
assert(userInfo != null),
2019-04-26 06:58:18 +00:00
assert(isLastMessage != null),
2019-04-26 09:04:06 +00:00
assert(isFirstMessage != null),
2019-04-25 06:56:16 +00:00
super(key: key);
2019-04-26 09:04:06 +00:00
/// Build account image
Widget _buildAccountImage() {
return Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.all(10.0),
child: AccountImageWidget(
user: userInfo,
width: 35.0,
2019-04-26 09:04:06 +00:00
),
);
}
2019-04-26 09:28:33 +00:00
/// Build widget image
2019-04-26 09:34:24 +00:00
Widget _buildMessageImage(BuildContext context) {
2019-04-26 09:28:33 +00:00
return Container(
margin: EdgeInsets.only(bottom: 2),
child: Material(
2019-04-26 09:34:24 +00:00
child: InkWell(
onTap: () {
showImageFullScreen(context, message.imageURL);
},
2019-04-26 09:34:24 +00:00
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(),
),
2019-04-26 09:28:33 +00:00
),
2019-04-26 09:34:24 +00:00
errorWidget: (c, s, o) => Container(
width: 200,
height: 200,
color: Colors.red,
child: Center(
child: Icon(
Icons.error,
color: Colors.white,
)),
2019-04-26 09:28:33 +00:00
),
2019-04-26 09:34:24 +00:00
),
2019-04-26 09:28:33 +00:00
),
borderRadius: BorderRadius.all(Radius.circular(8.0)),
clipBehavior: Clip.hardEdge,
),
);
}
2019-04-26 06:58:18 +00:00
/// 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),
2019-04-26 09:04:06 +00:00
textAlign: TextAlign.center,
2019-04-26 06:58:18 +00:00
),
)
: Container();
}
/// Build a message of the current user
2019-04-26 09:34:24 +00:00
Widget _buildRightMessage(BuildContext context) {
2019-04-26 06:58:18 +00:00
return Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 10.0),
2019-04-26 06:58:18 +00:00
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Column(
children: <Widget>[
2019-04-26 09:04:06 +00:00
Row(
children: <Widget>[
2019-04-26 09:28:33 +00:00
Column(
children: <Widget>[
// Text image
Container(
child: message.hasImage
? _buildMessageImage(context)
: null,
2019-04-26 09:28:33 +00:00
),
// Text message
Container(
child: message.hasMessage
? Container(
child: Text(
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,
),
],
2019-04-26 09:04:06 +00:00
),
// Account image
_buildAccountImage()
],
2019-04-26 06:58:18 +00:00
),
// Date
2019-04-26 09:04:06 +00:00
Container(
child: _buildMessageDate(),
margin: EdgeInsets.only(right: 45.0),
)
2019-04-26 06:58:18 +00:00
],
),
],
),
);
// Text
}
/// Build a message of a peer user
2019-04-26 09:34:24 +00:00
Widget _buildLeftMessage(BuildContext context) {
2019-04-26 06:58:18 +00:00
return Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 5.0),
2019-04-26 09:04:06 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2019-04-26 06:58:18 +00:00
children: <Widget>[
2019-04-26 09:04:06 +00:00
//User name
Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(left: 55.0),
2019-04-26 09:04:06 +00:00
child: isFirstMessage
? Text(
userInfo.fullName,
textAlign: TextAlign.left,
style: TextStyle(fontSize: 12.0),
)
: null,
),
Row(
2019-04-26 06:58:18 +00:00
children: <Widget>[
2019-04-26 09:04:06 +00:00
// Account image
_buildAccountImage(),
2019-04-26 06:58:18 +00:00
2019-04-26 09:04:06 +00:00
Column(
children: <Widget>[
2019-04-26 09:28:33 +00:00
// Text image
Container(
child:
message.hasImage ? _buildMessageImage(context) : null,
2019-04-26 09:28:33 +00:00
),
2019-04-26 09:04:06 +00:00
// Text message
Container(
2019-04-26 09:28:33 +00:00
child: message.hasMessage
? Container(
child: Text(
message.message,
textAlign: TextAlign.justify,
),
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,
2019-04-26 09:04:06 +00:00
),
],
),
2019-04-26 06:58:18 +00:00
],
),
2019-04-26 09:04:06 +00:00
// Date
Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.only(left: 50.0),
2019-04-26 09:04:06 +00:00
child: Align(
alignment: Alignment.topLeft,
child: _buildMessageDate(),
),
)
2019-04-26 06:58:18 +00:00
],
),
);
}
2019-04-25 06:56:16 +00:00
@override
Widget build(BuildContext context) {
2019-04-26 06:58:18 +00:00
return userID() == message.userID
2019-04-26 09:34:24 +00:00
? _buildRightMessage(context)
: _buildLeftMessage(context);
2019-04-25 06:56:16 +00:00
}
}