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

260 lines
8.0 KiB
Dart
Raw Normal View History

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-05-11 07:43:30 +00:00
import 'package:comunic/ui/widgets/network_image_widget.dart';
2020-04-16 13:10:47 +00:00
import 'package:comunic/ui/widgets/text_widget.dart';
2019-04-26 06:58:18 +00:00
import 'package:comunic/utils/date_utils.dart';
2019-05-04 06:58:14 +00:00
import 'package:comunic/utils/intl_utils.dart';
2019-11-01 13:17:46 +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
2019-05-04 08:24:38 +00:00
enum _MenuChoices { DELETE, REQUEST_UPDATE_CONTENT }
2019-05-04 06:58:14 +00:00
2019-05-04 08:24:38 +00:00
typedef OnRequestMessageUpdate = void Function(ConversationMessage);
2019-05-04 06:58:14 +00:00
typedef OnRequestMessageDelete = void Function(ConversationMessage);
2019-04-25 06:56:16 +00:00
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-05-04 08:24:38 +00:00
final OnRequestMessageUpdate onRequestMessageUpdate;
2019-05-04 06:58:14 +00:00
final OnRequestMessageDelete onRequestMessageDelete;
2019-04-25 06:56:16 +00:00
2019-05-04 06:58:14 +00:00
const ConversationMessageTile({
Key key,
@required this.message,
@required this.userInfo,
@required this.isLastMessage,
@required this.isFirstMessage,
2019-05-04 08:24:38 +00:00
@required this.onRequestMessageUpdate,
2019-05-04 06:58:14 +00:00
@required this.onRequestMessageDelete,
}) : assert(message != null),
2019-04-25 06:56:16 +00:00
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-05-04 08:24:38 +00:00
assert(onRequestMessageUpdate != null),
2019-05-04 06:58:14 +00:00
assert(onRequestMessageDelete != null),
2019-04-25 06:56:16 +00:00
super(key: key);
2019-04-26 09:04:06 +00:00
/// Build account image
2019-05-04 06:58:14 +00:00
Widget _buildAccountImage(BuildContext context) {
2019-04-26 09:04:06 +00:00
return Container(
2019-04-26 09:28:33 +00:00
margin: EdgeInsets.all(10.0),
2019-05-04 06:58:14 +00:00
child: PopupMenuButton<_MenuChoices>(
child: AccountImageWidget(
user: userInfo,
width: 35.0,
),
itemBuilder: (c) => [
2019-11-01 13:17:46 +00:00
// Update message content
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.REQUEST_UPDATE_CONTENT,
child: Text(tr("Update")),
),
2019-05-04 08:24:38 +00:00
2019-11-01 13:17:46 +00:00
// Delete the message
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.DELETE,
child: Text(tr("Delete")),
),
],
2019-05-04 06:58:14 +00:00
onSelected: _menuOptionSelected,
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),
2019-05-11 07:43:30 +00:00
child: NetworkImageWidget(
url: message.imageURL,
allowFullScreen: true,
width: 200,
height: 200,
2019-04-26 09:28:33 +00:00
),
);
}
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(
margin: EdgeInsets.only(bottom: isLastMessage ? 20.0 : 2.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(
width: 200.0,
alignment: Alignment.centerRight,
child: Container(
2020-04-16 13:10:47 +00:00
child: TextWidget(
content: message.message,
textAlign: TextAlign.justify,
style: TextStyle(color: Colors.white),
),
padding: EdgeInsets.fromLTRB(
15.0, 10.0, 15.0, 10.0),
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(8.0),
),
2019-04-26 09:28:33 +00:00
),
)
: null,
),
],
2019-04-26 09:04:06 +00:00
),
// Account image
2019-05-04 06:58:14 +00:00
_buildAccountImage(context)
2019-04-26 09:04:06 +00:00
],
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
2019-05-04 06:58:14 +00:00
_buildAccountImage(context),
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(
width: 200.0,
alignment: Alignment.centerLeft,
child: Container(
2020-04-16 13:10:47 +00:00
child: TextWidget(
content: message.message,
textAlign: TextAlign.justify,
2019-11-01 13:17:46 +00:00
style: TextStyle(
color: darkTheme()
? Colors.white
: Colors.black),
),
padding:
EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
decoration: BoxDecoration(
2019-11-01 13:17:46 +00:00
color: darkTheme()
? Colors.white12
: Colors.black12,
borderRadius: BorderRadius.circular(8.0),
),
2019-04-26 09:28:33 +00:00
),
)
: 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-05-04 06:58:14 +00:00
return message.isOwner
2019-04-26 09:34:24 +00:00
? _buildRightMessage(context)
: _buildLeftMessage(context);
2019-04-25 06:56:16 +00:00
}
2019-05-04 06:58:14 +00:00
/// Process menu choice
void _menuOptionSelected(_MenuChoices value) {
2019-05-04 08:24:38 +00:00
switch (value) {
case _MenuChoices.REQUEST_UPDATE_CONTENT:
onRequestMessageUpdate(message);
break;
2019-05-04 06:58:14 +00:00
case _MenuChoices.DELETE:
onRequestMessageDelete(message);
break;
}
}
2019-04-25 06:56:16 +00:00
}