您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
156 行
5.8 KiB
156 行
5.8 KiB
using System.Collections.Generic;
|
|
using System.Text.RegularExpressions;
|
|
using ChatComponents;
|
|
using Unity.UIWidgets.foundation;
|
|
using Unity.UIWidgets.material;
|
|
using Unity.UIWidgets.painting;
|
|
using Unity.UIWidgets.rendering;
|
|
using Unity.UIWidgets.widgets;
|
|
|
|
namespace UIWidgetsSample
|
|
{
|
|
internal class TextMessage : StatelessWidget
|
|
{
|
|
/// [types.TextMessage]
|
|
public readonly ChatComponents.TextMessage message;
|
|
|
|
/// See [LinkPreview.onPreviewDataFetched]
|
|
public readonly OnPreviewDataFetched
|
|
onPreviewDataFetched;
|
|
|
|
/// Show user name for the received message. Useful for a group chat.
|
|
public readonly bool showName;
|
|
|
|
/// Enables link (URL) preview
|
|
public readonly bool usePreviewData;
|
|
|
|
/// Creates a text message widget from a [types.TextMessage] class
|
|
public TextMessage(
|
|
ChatComponents.TextMessage message,
|
|
bool usePreviewData,
|
|
bool showName,
|
|
Key key = null,
|
|
OnPreviewDataFetched onPreviewDataFetched = null
|
|
) : base(key)
|
|
{
|
|
this.message = message;
|
|
this.usePreviewData = usePreviewData;
|
|
this.showName = showName;
|
|
this.onPreviewDataFetched = onPreviewDataFetched;
|
|
}
|
|
|
|
private void _onPreviewDataFetched(PreviewData previewData)
|
|
{
|
|
if (message.previewData == null)
|
|
onPreviewDataFetched?.Invoke(previewData,message);
|
|
}
|
|
|
|
private Widget _linkPreview(
|
|
User user,
|
|
float width,
|
|
BuildContext context
|
|
)
|
|
{
|
|
var bodyTextStyle = user.id == message.author.id
|
|
? InheritedChatTheme.of(context).theme.sentMessageBodyTextStyle
|
|
: InheritedChatTheme.of(context).theme.receivedMessageBodyTextStyle;
|
|
var linkDescriptionTextStyle = user.id == message.author.id
|
|
? InheritedChatTheme.of(context)
|
|
.theme
|
|
.sentMessageLinkDescriptionTextStyle
|
|
: InheritedChatTheme.of(context)
|
|
.theme
|
|
.receivedMessageLinkDescriptionTextStyle;
|
|
var linkTitleTextStyle = user.id == message.author.id
|
|
? InheritedChatTheme.of(context).theme.sentMessageLinkTitleTextStyle
|
|
: InheritedChatTheme.of(context)
|
|
.theme
|
|
.receivedMessageLinkTitleTextStyle;
|
|
var test = InheritedChatTheme.of(context);
|
|
var color = ChatUtils.getUserAvatarNameColor(message.author, InheritedChatTheme.of(context).theme.userAvatarNameColors);
|
|
var name = ChatUtils.getUserName(message.author);
|
|
|
|
return new LinkPreview(
|
|
width: width,
|
|
enableAnimation: true,
|
|
header: showName ? name : null,
|
|
headerStyle: InheritedChatTheme.of(context)
|
|
.theme
|
|
.userNameTextStyle
|
|
.copyWith(color: color),
|
|
linkStyle: bodyTextStyle,
|
|
metadataTextStyle: linkDescriptionTextStyle,
|
|
metadataTitleStyle: linkTitleTextStyle,
|
|
onPreviewDataFetched: (previewData,textMessage)=>
|
|
{
|
|
_onPreviewDataFetched(previewData);
|
|
},
|
|
padding: EdgeInsets.symmetric(
|
|
horizontal: 24,
|
|
vertical: 16
|
|
),
|
|
previewData: message.previewData,
|
|
text: message.text,
|
|
textStyle: bodyTextStyle
|
|
|
|
);
|
|
}
|
|
|
|
private Widget _textWidget(User user, BuildContext context)
|
|
{
|
|
var color = ChatUtils.getUserAvatarNameColor(message.author,
|
|
InheritedChatTheme.of(context).theme.userAvatarNameColors);
|
|
var name = ChatUtils.getUserName(message.author);
|
|
var results = new List<Widget>();
|
|
if (showName)
|
|
results.Add(new Padding(
|
|
padding: EdgeInsets.only(bottom: 6.0f),
|
|
child: new Text(
|
|
name,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: InheritedChatTheme.of(context)
|
|
.theme
|
|
.userNameTextStyle
|
|
.copyWith(color: color)
|
|
)
|
|
));
|
|
var test = InheritedChatTheme.of(context);
|
|
var style = user.id == message.author.id
|
|
? InheritedChatTheme.of(context).theme.sentMessageBodyTextStyle
|
|
: InheritedChatTheme.of(context)
|
|
.theme
|
|
.receivedMessageBodyTextStyle;
|
|
results.Add(//new SelectableText(
|
|
new Text(
|
|
data: message.text,
|
|
style: style ,
|
|
textWidthBasis: TextWidthBasis.longestLine
|
|
));
|
|
return new Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: results
|
|
);
|
|
}
|
|
|
|
public override Widget build(BuildContext context)
|
|
{
|
|
var _user = InheritedUser.of(context).user;
|
|
var _width = MediaQuery.of(context).size.width;
|
|
|
|
var urlRegexp = new Regex(ChatUtils.REGEX_LINK);
|
|
var matches = urlRegexp.Match(message.text.ToLower());
|
|
|
|
if (matches.Length !=0 && usePreviewData && onPreviewDataFetched != null)
|
|
return _linkPreview(_user, _width, context);
|
|
|
|
return new Container(
|
|
margin: EdgeInsets.symmetric(
|
|
horizontal: 24,
|
|
vertical: 16
|
|
),
|
|
child: _textWidget(_user, context)
|
|
);
|
|
}
|
|
}
|
|
}
|