diff --git a/lib/readmore.dart b/lib/readmore.dart index d237641..01904d6 100644 --- a/lib/readmore.dart +++ b/lib/readmore.dart @@ -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 { @@ -176,6 +176,7 @@ class ReadMoreTextState extends State { final TapGestureRecognizer _recognizer = TapGestureRecognizer(); ValueNotifier? _isCollapsed; + ValueNotifier get _effectiveIsCollapsed => widget.isCollapsed ?? (_isCollapsed ??= ValueNotifier(true)); @@ -319,7 +320,7 @@ class ReadMoreTextState extends State { 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!], @@ -338,7 +339,7 @@ class ReadMoreTextState extends State { final text = TextSpan( children: [ if (preTextSpan != null) preTextSpan, - dataTextSpan, + _removeWidgetSpan(dataTextSpan), if (postTextSpan != null) postTextSpan, ], ); @@ -516,7 +517,7 @@ class ReadMoreTextState extends State { return TextSpan(text: data, style: textStyle); } - final contents = []; + final contents = []; data.splitMapJoin( regExp, @@ -540,7 +541,7 @@ class ReadMoreTextState extends State { textStyle: textStyle, ); - assert(_isTextSpan(content)); + // assert(_isTextSpan(content)); contents.add(content); return ''; @@ -574,7 +575,8 @@ class ReadMoreTextState extends State { final nextSpan = TextSpan( text: newText, - children: null, // remove potential children + children: null, + // remove potential children style: textSpan.style, recognizer: textSpan.recognizer, mouseCursor: textSpan.mouseCursor, @@ -624,7 +626,8 @@ class ReadMoreTextState extends State { final resultTextSpan = didTrim ? TextSpan( text: textSpan.text, - children: newChildren, // update children + children: newChildren, + // update children style: textSpan.style, recognizer: textSpan.recognizer, mouseCursor: textSpan.mouseCursor, @@ -643,17 +646,15 @@ class ReadMoreTextState extends State { ); } - 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, + ); } }