fluffychat/lib/pages/chat/events/message.dart

291 lines
11 KiB
Dart
Raw Normal View History

2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
import 'package:matrix/matrix.dart';
2021-10-26 18:50:34 +02:00
2021-01-16 12:46:38 +01:00
import 'package:fluffychat/config/themes.dart';
2020-01-09 12:16:34 +01:00
import 'package:fluffychat/utils/date_time_extension.dart';
2020-01-18 13:22:22 +01:00
import 'package:fluffychat/utils/string_color.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/widgets/avatar.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../../../config/app_config.dart';
import 'message_content.dart';
import 'message_reactions.dart';
2021-11-09 21:32:16 +01:00
import 'reply_content.dart';
2020-01-01 19:10:13 +01:00
import 'state_message.dart';
import 'verification_request_content.dart';
2020-01-01 19:10:13 +01:00
class Message extends StatelessWidget {
final Event event;
2020-01-17 10:39:46 +01:00
final Event nextEvent;
final void Function(Event) onSelect;
final void Function(Event) onAvatarTab;
2021-11-13 13:06:36 +01:00
final void Function(Event) onInfoTab;
final void Function(String) scrollToEventId;
final void Function(String) unfold;
2020-02-09 15:15:29 +01:00
final bool longPressSelect;
final bool selected;
2020-02-11 12:49:39 +01:00
final Timeline timeline;
2020-01-01 19:10:13 +01:00
2020-02-09 15:15:29 +01:00
const Message(this.event,
2020-02-11 12:49:39 +01:00
{this.nextEvent,
this.longPressSelect,
this.onSelect,
2021-11-13 13:06:36 +01:00
this.onInfoTab,
2020-04-08 12:38:52 +02:00
this.onAvatarTab,
2020-09-19 19:21:33 +02:00
this.scrollToEventId,
@required this.unfold,
2020-02-11 12:49:39 +01:00
this.selected,
2021-10-14 18:09:30 +02:00
this.timeline,
Key key})
: super(key: key);
2020-01-01 19:10:13 +01:00
2020-06-20 11:46:12 +02:00
/// Indicates wheither the user may use a mouse instead
/// of touchscreen.
static bool useMouse = false;
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2020-02-21 09:45:37 +01:00
if (![EventTypes.Message, EventTypes.Sticker, EventTypes.Encrypted]
.contains(event.type)) {
return StateMessage(event, unfold: unfold);
}
2020-01-01 19:10:13 +01:00
if (event.type == EventTypes.Message &&
event.messageType == EventTypes.KeyVerificationRequest) {
return VerificationRequestContent(event: event, timeline: timeline);
}
2021-04-14 10:37:15 +02:00
final client = Matrix.of(context).client;
2020-05-13 15:58:59 +02:00
final ownMessage = event.senderId == client.userID;
2021-04-14 10:37:15 +02:00
final alignment = ownMessage ? Alignment.topRight : Alignment.topLeft;
2021-11-13 13:06:36 +01:00
var color = Theme.of(context).scaffoldBackgroundColor;
final displayTime = event.type == EventTypes.RoomCreate ||
nextEvent == null ||
!event.originServerTs.sameEnvironment(nextEvent.originServerTs);
2020-05-13 15:58:59 +02:00
final sameSender = nextEvent != null &&
2021-11-13 13:06:36 +01:00
[
EventTypes.Message,
EventTypes.Sticker,
EventTypes.Encrypted,
].contains(nextEvent.type)
? nextEvent.sender.id == event.sender.id && !displayTime
: false;
2021-11-13 13:06:36 +01:00
final textColor = ownMessage
? Colors.white
: Theme.of(context).brightness == Brightness.dark
? Colors.white
: Colors.black;
2021-04-14 10:37:15 +02:00
final rowMainAxisAlignment =
2020-01-01 19:10:13 +01:00
ownMessage ? MainAxisAlignment.end : MainAxisAlignment.start;
final displayEvent = event.getDisplayEvent(timeline);
2021-11-13 13:06:36 +01:00
final borderRadius = BorderRadius.only(
topLeft: !ownMessage
? const Radius.circular(2)
: const Radius.circular(AppConfig.borderRadius),
topRight: ownMessage
? const Radius.circular(2)
: const Radius.circular(AppConfig.borderRadius),
bottomLeft: const Radius.circular(AppConfig.borderRadius),
bottomRight: const Radius.circular(AppConfig.borderRadius),
);
final noBubble = {
MessageTypes.Video,
MessageTypes.Image,
MessageTypes.Sticker,
}.contains(event.messageType);
2021-11-13 13:06:36 +01:00
if (ownMessage) {
2021-10-25 10:46:58 +02:00
color = displayEvent.status.isError
2020-01-03 11:57:00 +01:00
? Colors.redAccent
: Theme.of(context).primaryColor;
2020-01-01 19:10:13 +01:00
}
2021-04-14 10:37:15 +02:00
final rowChildren = <Widget>[
2021-11-13 13:06:36 +01:00
sameSender || ownMessage
? SizedBox(
width: Avatar.defaultSize,
height: Avatar.defaultSize,
child: event.status == EventStatus.sending
? const Center(
child: SizedBox(
width: 16,
height: 16,
child:
CircularProgressIndicator.adaptive(strokeWidth: 2),
),
)
: null,
)
: Avatar(
event.sender.avatarUrl,
event.sender.calcDisplayname(),
onTap: () => onAvatarTab(event),
),
2020-01-01 19:10:13 +01:00
Expanded(
2021-11-13 13:06:36 +01:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
if (!ownMessage && !sameSender)
Padding(
padding: const EdgeInsets.only(left: 8.0, bottom: 4),
child: event.room.isDirectChat
? const SizedBox(height: 12)
: Text(
event.sender.calcDisplayname(),
style: TextStyle(
fontSize: 12,
fontWeight: FontWeight.bold,
color: event.sender.calcDisplayname().color,
),
),
),
Container(
alignment: alignment,
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Material(
color: noBubble ? null : color,
elevation: noBubble ? 0 : 6,
2021-11-13 13:06:36 +01:00
shadowColor:
Theme.of(context).secondaryHeaderColor.withAlpha(100),
borderRadius: borderRadius,
clipBehavior: Clip.antiAlias,
2021-11-13 13:06:36 +01:00
child: InkWell(
onHover: (b) => useMouse = true,
onTap: !useMouse && longPressSelect
? () => null
: () => onSelect(event),
onLongPress: !longPressSelect ? null : () => onSelect(event),
borderRadius: borderRadius,
child: Container(
decoration: BoxDecoration(
borderRadius:
BorderRadius.circular(AppConfig.borderRadius),
),
padding:
noBubble ? EdgeInsets.zero : const EdgeInsets.all(16),
2021-11-13 13:06:36 +01:00
constraints: const BoxConstraints(
maxWidth: FluffyThemes.columnWidth * 1.5),
child: Stack(
children: <Widget>[
2021-11-13 13:06:36 +01:00
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
if (event.relationshipType ==
RelationshipTypes.reply)
FutureBuilder<Event>(
future: event.getReplyEvent(timeline),
builder: (BuildContext context, snapshot) {
final replyEvent = snapshot.hasData
? snapshot.data
: Event(
eventId: event.relationshipEventId,
content: {
'msgtype': 'm.text',
'body': '...'
},
senderId: event.senderId,
type: 'm.room.message',
room: event.room,
status: EventStatus.sent,
originServerTs: DateTime.now(),
);
return InkWell(
onTap: () {
if (scrollToEventId != null) {
scrollToEventId(replyEvent.eventId);
}
},
child: AbsorbPointer(
child: Container(
margin: const EdgeInsets.symmetric(
vertical: 4.0),
child: ReplyContent(replyEvent,
lightText: ownMessage,
timeline: timeline),
),
),
);
},
2021-11-13 13:06:36 +01:00
),
MessageContent(
displayEvent,
textColor: textColor,
onInfoTab: onInfoTab,
),
],
),
],
2020-04-08 12:38:52 +02:00
),
2021-11-13 13:06:36 +01:00
),
2020-05-15 19:57:53 +02:00
),
),
2020-05-15 19:57:53 +02:00
),
2021-11-13 13:06:36 +01:00
],
2020-01-01 19:10:13 +01:00
),
),
];
final row = Row(
2021-11-13 13:06:36 +01:00
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: rowMainAxisAlignment,
children: rowChildren,
);
Widget container;
2021-11-13 13:06:36 +01:00
if (event.hasAggregatedEvents(timeline, RelationshipTypes.reaction) ||
displayTime) {
container = Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment:
ownMessage ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: <Widget>[
2021-11-13 13:06:36 +01:00
if (displayTime)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Center(
child: Material(
color: Theme.of(context).backgroundColor,
borderRadius: BorderRadius.circular(AppConfig.borderRadius / 2),
2021-11-13 20:17:11 +01:00
clipBehavior: Clip.antiAlias,
2021-11-13 13:06:36 +01:00
child: Padding(
padding: const EdgeInsets.all(6.0),
child: Text(
event.originServerTs.localizedTime(context),
2021-11-13 19:22:11 +01:00
style: TextStyle(fontSize: 14 * AppConfig.fontSizeFactor),
2021-11-13 13:06:36 +01:00
),
),
)),
),
row,
Padding(
padding: EdgeInsets.only(
top: 4.0,
left: (ownMessage ? 0 : Avatar.defaultSize) + 12.0,
2021-11-13 13:06:36 +01:00
right: 12.0,
),
child: MessageReactions(event, timeline),
),
],
);
} else {
container = row;
}
2020-01-17 10:39:46 +01:00
2021-05-24 11:22:03 +02:00
return Center(
child: Container(
color: selected
? Theme.of(context).primaryColor.withAlpha(100)
: Theme.of(context).primaryColor.withAlpha(0),
2021-10-14 18:09:30 +02:00
constraints:
const BoxConstraints(maxWidth: FluffyThemes.columnWidth * 2.5),
2021-05-24 11:22:03 +02:00
child: Padding(
2021-10-14 18:09:30 +02:00
padding: const EdgeInsets.only(
left: 8.0, right: 8.0, bottom: 4.0, top: 4.0),
2021-05-24 11:22:03 +02:00
child: container,
),
2020-01-01 19:10:13 +01:00
),
);
}
}