From 3f779072e3d7a528c906ab594c76493f04094c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20M=C3=BCller?= Date: Tue, 5 Mar 2024 19:21:12 +0100 Subject: [PATCH] #36 Fixed overflowing message time --- lib/extensions/text.dart | 12 ++++++ .../pages/talk/components/chatBubble.dart | 41 +++++++++---------- 2 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 lib/extensions/text.dart diff --git a/lib/extensions/text.dart b/lib/extensions/text.dart new file mode 100644 index 0000000..caaabe1 --- /dev/null +++ b/lib/extensions/text.dart @@ -0,0 +1,12 @@ +import 'package:flutter/material.dart'; + +extension TextExt on Text { + Size get size { + final TextPainter textPainter = TextPainter( + text: TextSpan(text: data, style: style), + maxLines: 1, + textDirection: TextDirection.ltr + )..layout(minWidth: 0, maxWidth: double.infinity); + return textPainter.size; + } +} \ No newline at end of file diff --git a/lib/view/pages/talk/components/chatBubble.dart b/lib/view/pages/talk/components/chatBubble.dart index d6aca90..1393c0a 100644 --- a/lib/view/pages/talk/components/chatBubble.dart +++ b/lib/view/pages/talk/components/chatBubble.dart @@ -5,6 +5,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:jiffy/jiffy.dart'; +import 'package:marianum_mobile/extensions/text.dart'; import 'package:provider/provider.dart'; import '../../../../api/marianumcloud/talk/chat/getChatResponse.dart'; @@ -80,15 +81,6 @@ class _ChatBubbleState extends State { double downloadProgress = 0; Future? downloadCore; - Size _textSize(String text, TextStyle style) { - final TextPainter textPainter = TextPainter( - text: TextSpan(text: text, style: style), - maxLines: 1, - textDirection: TextDirection.ltr) - ..layout(minWidth: 0, maxWidth: double.infinity); - return textPainter.size; - } - BubbleStyle getStyle() { if(widget.bubbleData.messageType == GetRoomResponseObjectMessageType.comment) { if(widget.isSender) { @@ -107,7 +99,19 @@ class _ChatBubbleState extends State { message = ChatMessage(originalMessage: widget.bubbleData.message, originalData: widget.bubbleData.messageParameters); bool showActorDisplayName = widget.bubbleData.messageType == GetRoomResponseObjectMessageType.comment && widget.chatData.type != GetRoomResponseObjectConversationType.oneToOne; bool showBubbleTime = widget.bubbleData.messageType != GetRoomResponseObjectMessageType.system; - var actorTextStyle = TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold); + + Text actorText = Text( + widget.bubbleData.actorDisplayName, + textAlign: TextAlign.start, + overflow: TextOverflow.ellipsis, + style: TextStyle(color: Theme.of(context).primaryColor, fontWeight: FontWeight.bold), + ); + + Text timeText = Text( + Jiffy.parseFromMillisecondsSinceEpoch(widget.bubbleData.timestamp * 1000).format(pattern: "HH:mm"), + textAlign: TextAlign.end, + style: const TextStyle(color: Colors.grey, fontSize: 12), + ); return Column( mainAxisSize: MainAxisSize.min, @@ -122,7 +126,9 @@ class _ChatBubbleState extends State { child: Container( constraints: BoxConstraints( maxWidth: MediaQuery.of(context).size.width * 0.9, - minWidth: showActorDisplayName ? _textSize(widget.bubbleData.actorDisplayName, actorTextStyle).width : 30, + minWidth: showActorDisplayName + ? actorText.size.width + : timeText.size.width, ), child: Stack( children: [ @@ -135,12 +141,7 @@ class _ChatBubbleState extends State { child: Positioned( top: 0, left: 0, - child: Text( - widget.bubbleData.actorDisplayName, - textAlign: TextAlign.start, - overflow: TextOverflow.ellipsis, - style: actorTextStyle, - ), + child: actorText ), ), Visibility( @@ -148,11 +149,7 @@ class _ChatBubbleState extends State { child: Positioned( bottom: 0, right: 0, - child: Text( - Jiffy.parseFromMillisecondsSinceEpoch(widget.bubbleData.timestamp * 1000).format(pattern: "HH:mm"), - textAlign: TextAlign.end, - style: const TextStyle(color: Colors.grey, fontSize: 12), - ), + child: timeText ), ), Visibility(