Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/expandable_text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class ExpandableText extends StatefulWidget {
this.animationDuration,
this.animationCurve,
this.semanticsLabel,
this.mentionsWithSpace
}) : assert(maxLines > 0),
super(key: key);

Expand Down Expand Up @@ -73,6 +74,7 @@ class ExpandableText extends StatefulWidget {
final Duration? animationDuration;
final Curve? animationCurve;
final String? semanticsLabel;
final bool? mentionsWithSpace;

@override
ExpandableTextState createState() => ExpandableTextState();
Expand Down Expand Up @@ -248,7 +250,7 @@ class ExpandableTextState extends State<ExpandableText>
_expanded
? _textSegments
: parseText(
widget.text.substring(0, max(endOffset, 0))),
widget.text.substring(0, max(endOffset, 0)), widget.mentionsWithSpace ?? false),
effectiveTextStyle!,
recognizer),
)
Expand Down Expand Up @@ -318,7 +320,7 @@ class ExpandableTextState extends State<ExpandableText>
return;
}

_textSegments = parseText(widget.text);
_textSegments = parseText(widget.text, widget.mentionsWithSpace ?? false);

_textSegments.forEach((element) {
if (element.isUrl && widget.onUrlTap != null) {
Expand Down Expand Up @@ -377,4 +379,4 @@ class ExpandableTextState extends State<ExpandableText>

return spans;
}
}
}
14 changes: 10 additions & 4 deletions lib/text_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TextSegment {
[this.name,
this.isHashtag = false,
this.isMention = false,
this.isUrl = false]);
this.isUrl = false,]);

@override
bool operator ==(Object other) =>
Expand All @@ -38,20 +38,24 @@ class TextSegment {
///
/// Mentions are all words that start with @, e.g. @mention.
/// Hashtags are all words that start with #, e.g. #hashtag.
List<TextSegment> parseText(String? text) {
List<TextSegment> parseText(String? text, bool withSpace) {
final segments = <TextSegment>[];

if (text == null || text.isEmpty) {
return segments;
}

// parse urls and words starting with @ (mention) or # (hashtag)
RegExp exp = RegExp(
RegExp exp = withSpace ?RegExp(
r'(?<keyword>(#|@)([\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]+)(?:\s[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]+)|(?<url>(?:(?:https?|ftp):\/\/)?[-a-z0-9@:%._\+~#=]{1,256}\.[a-z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?))',
unicode: true)
:RegExp(
r'(?<keyword>(#|@)([\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}\p{Join_Control}]+)|(?<url>(?:(?:https?|ftp):\/\/)?[-a-z0-9@:%._\+~#=]{1,256}\.[a-z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?))',
unicode: true);
final matches = exp.allMatches(text);

var start = 0;
String previousKeyword = "";
matches.forEach((match) {
// text before the keyword
if (match.start > start) {
Expand All @@ -65,6 +69,7 @@ List<TextSegment> parseText(String? text) {

final url = match.namedGroup('url');
final keyword = match.namedGroup('keyword');
previousKeyword = keyword ?? "";

if (url != null) {
segments.add(TextSegment(url, url, false, false, true));
Expand All @@ -76,7 +81,8 @@ List<TextSegment> parseText(String? text) {
}

final isHashtag = keyword.startsWith('#');
final isMention = keyword.startsWith('@');
final isMention = keyword.startsWith('@') || previousKeyword.startsWith('@');


segments.add(
TextSegment(keyword, keyword.substring(1), isHashtag, isMention));
Expand Down