1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 21:08:52 +00:00
comunicmobile/lib/ui/tiles/conversation_message_tile.dart

261 lines
8.1 KiB
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/network_image_widget.dart';
import 'package:comunic/ui/widgets/text_widget.dart';
import 'package:comunic/utils/date_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
/// Conversation message tile
///
/// @author Pierre HUBERT
enum _MenuChoices { DELETE, REQUEST_UPDATE_CONTENT }
typedef OnRequestMessageUpdate = void Function(ConversationMessage);
typedef OnRequestMessageDelete = void Function(ConversationMessage);
class ConversationMessageTile extends StatelessWidget {
final ConversationMessage message;
final User userInfo;
final bool isLastMessage;
final bool isFirstMessage;
final OnRequestMessageUpdate onRequestMessageUpdate;
final OnRequestMessageDelete onRequestMessageDelete;
const ConversationMessageTile({
Key key,
@required this.message,
@required this.userInfo,
@required this.isLastMessage,
@required this.isFirstMessage,
@required this.onRequestMessageUpdate,
@required this.onRequestMessageDelete,
}) : assert(message != null),
assert(userInfo != null),
assert(isLastMessage != null),
assert(isFirstMessage != null),
assert(onRequestMessageUpdate != null),
assert(onRequestMessageDelete != null),
super(key: key);
/// Build account image
Widget _buildAccountImage(BuildContext context) {
return Container(
margin: EdgeInsets.all(10.0),
child: PopupMenuButton<_MenuChoices>(
child: AccountImageWidget(
user: userInfo,
width: 35.0,
),
itemBuilder: (c) => [
// Update message content
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.REQUEST_UPDATE_CONTENT,
child: Text(tr("Update")),
),
// Delete the message
PopupMenuItem(
enabled: message.isOwner,
value: _MenuChoices.DELETE,
child: Text(tr("Delete")),
),
],
onSelected: _menuOptionSelected,
),
);
}
/// Build widget image
Widget _buildMessageImage(BuildContext context) {
return Container(
margin: EdgeInsets.only(bottom: 2),
child: NetworkImageWidget(
url: message.imageURL,
allowFullScreen: true,
width: 200,
height: 200,
),
);
}
/// 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 : 2.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Column(
children: <Widget>[
Row(
children: <Widget>[
Column(
children: <Widget>[
// Text image
Container(
child: message.hasImage
? _buildMessageImage(context)
: null,
),
// Text message
Container(
child: message.hasMessage
? Container(
width: 200.0,
alignment: Alignment.centerRight,
child: Container(
child: TextWidget(
content: message.message,
textAlign: TextAlign.justify,
style: TextStyle(color: Colors.white),
linksColor: Colors.indigo,
),
padding: EdgeInsets.fromLTRB(
15.0, 10.0, 15.0, 10.0),
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(8.0),
),
),
)
: null,
),
],
),
// Account image
_buildAccountImage(context)
],
),
// 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: <Widget>[
//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: <Widget>[
// Account image
_buildAccountImage(context),
Column(
children: <Widget>[
// Text image
Container(
child:
message.hasImage ? _buildMessageImage(context) : null,
),
// Text message
Container(
child: message.hasMessage
? Container(
width: 200.0,
alignment: Alignment.centerLeft,
child: Container(
child: TextWidget(
content: message.message,
textAlign: TextAlign.justify,
style: TextStyle(
color: darkTheme()
? Colors.white
: Colors.black),
),
padding:
EdgeInsets.fromLTRB(15.0, 10.0, 15.0, 10.0),
decoration: BoxDecoration(
color: darkTheme()
? Colors.white12
: 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 message.isOwner
? _buildRightMessage(context)
: _buildLeftMessage(context);
}
/// Process menu choice
void _menuOptionSelected(_MenuChoices value) {
switch (value) {
case _MenuChoices.REQUEST_UPDATE_CONTENT:
onRequestMessageUpdate(message);
break;
case _MenuChoices.DELETE:
onRequestMessageDelete(message);
break;
}
}
}