Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a feature to allow WidgetSpan in annotation #70

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
39 changes: 20 additions & 19 deletions lib/readmore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class Annotation {
});

final RegExp regExp;
final TextSpan Function({required String text, required TextStyle textStyle})
spanBuilder;
final InlineSpan Function(
{required String text, required TextStyle textStyle}) spanBuilder;
}

class ReadMoreText extends StatefulWidget {
Expand Down Expand Up @@ -176,6 +176,7 @@ class ReadMoreTextState extends State<ReadMoreText> {
final TapGestureRecognizer _recognizer = TapGestureRecognizer();

ValueNotifier<bool>? _isCollapsed;

ValueNotifier<bool> get _effectiveIsCollapsed =>
widget.isCollapsed ?? (_isCollapsed ??= ValueNotifier(true));

Expand Down Expand Up @@ -319,7 +320,7 @@ class ReadMoreTextState extends State<ReadMoreText> {
final TextSpan dataTextSpan;
// Constructed by ReadMoreText.rich(...)
if (widget.richData != null) {
assert(_isTextSpan(widget.richData!));
// assert(_isTextSpan(widget.richData!));
dataTextSpan = TextSpan(
style: effectiveTextStyle,
children: [widget.richData!],
Expand All @@ -338,7 +339,7 @@ class ReadMoreTextState extends State<ReadMoreText> {
final text = TextSpan(
children: [
if (preTextSpan != null) preTextSpan,
dataTextSpan,
_removeWidgetSpan(dataTextSpan),
if (postTextSpan != null) postTextSpan,
],
);
Expand Down Expand Up @@ -516,7 +517,7 @@ class ReadMoreTextState extends State<ReadMoreText> {
return TextSpan(text: data, style: textStyle);
}

final contents = <TextSpan>[];
final contents = <InlineSpan>[];

data.splitMapJoin(
regExp,
Expand All @@ -540,7 +541,7 @@ class ReadMoreTextState extends State<ReadMoreText> {
textStyle: textStyle,
);

assert(_isTextSpan(content));
// assert(_isTextSpan(content));
contents.add(content);

return '';
Expand Down Expand Up @@ -574,7 +575,8 @@ class ReadMoreTextState extends State<ReadMoreText> {

final nextSpan = TextSpan(
text: newText,
children: null, // remove potential children
children: null,
// remove potential children
style: textSpan.style,
recognizer: textSpan.recognizer,
mouseCursor: textSpan.mouseCursor,
Expand Down Expand Up @@ -624,7 +626,8 @@ class ReadMoreTextState extends State<ReadMoreText> {
final resultTextSpan = didTrim
? TextSpan(
text: textSpan.text,
children: newChildren, // update children
children: newChildren,
// update children
style: textSpan.style,
recognizer: textSpan.recognizer,
mouseCursor: textSpan.mouseCursor,
Expand All @@ -643,17 +646,15 @@ class ReadMoreTextState extends State<ReadMoreText> {
);
}

bool _isTextSpan(InlineSpan span) {
if (span is! TextSpan) {
return false;
}

final children = span.children;
if (children == null || children.isEmpty) {
return true;
}

return children.every(_isTextSpan);
InlineSpan _removeWidgetSpan(TextSpan span) {
return TextSpan(
children: [
...?span.children
?.where((e) => e is! WidgetSpan)
.map((e) => _removeWidgetSpan(e as TextSpan))
],
style: span.style,
);
}
}

Expand Down