-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
WidgetSpan causes an error #61
Comments
Any updates on this? |
No, unfortunately I failed with my attempt to support |
I have the same issue |
Bump: Someone rescue this please |
So I've been playing around with this, since I had to support superscript/subscript... I've come up with a solution that works for our use case, but I hope this can help someone to make something that would work for anyone! So in our case I used a tag parser heavily inspired by https://github.com/gmetekorkmaz/TextManip , with our superscript tag being defined as follows: /// Displays text surrounded by `<sup></sup>` with a smaller fontSize (75% of original) at an [Offset] (40% above middle).
static final OffsetTag superscript = OffsetTag(
regExp: RegExp(r"<sup>(.*?)</sup>"),
callback: (RegExpMatch regExpMatch, TextStyle parentStyle) {
final double? parentFontSize = parentStyle.fontSize;
if (parentFontSize == null) {
throw ArgumentError.notNull('parentStyle.fontSize');
}
return ParsedOffsetText(
text: regExpMatch.group(1)!,
parentStyle: parentStyle,
style: TextStyle(
fontSize: parentFontSize * 0.75,
),
offset: Offset(0, -parentFontSize * 0.4),
);
}); then in the parser we go through all the tags in the specified final ParsedOffsetText offsetText = textManipTag.callback(regExpMatch, currentStyle) as ParsedOffsetText;
children.add(WidgetSpan(
child: Transform.translate(
offset: offsetText.offset,
child: Text(
offsetText.text,
style: currentStyle.merge(offsetText.style),
),
),
alignment: PlaceholderAlignment.middle,
)); Then to get rid of the bool _checkTextFits(TextSpan text, double scale, int? maxLines, BoxConstraints constraints) {
if (!widget.wrapWords) {
final words = text.toPlainText().split(RegExp('\\s+'));
final wordWrapTextPainter = TextPainter(
text: TextSpan(
style: text.style,
text: words.join('\n'),
),
textAlign: widget.textAlign ?? TextAlign.left,
textDirection: widget.textDirection ?? TextDirection.ltr,
textScaleFactor: scale,
maxLines: words.length,
locale: widget.locale,
strutStyle: widget.strutStyle,
);
wordWrapTextPainter.layout(maxWidth: constraints.maxWidth);
if (wordWrapTextPainter.didExceedMaxLines || wordWrapTextPainter.width > constraints.maxWidth) {
return false;
}
}
final textPainter = TextPainter(
text: text,
textAlign: widget.textAlign ?? TextAlign.left,
textDirection: widget.textDirection ?? TextDirection.ltr,
textScaleFactor: scale,
maxLines: maxLines,
locale: widget.locale,
strutStyle: widget.strutStyle,
);
final List<WidgetSpan> widgetSpans = _findWidgetSpans(text);
if (widgetSpans.isNotEmpty) {
textPainter.setPlaceholderDimensions(
widgetSpans.map((widgetSpan) {
final Transform transform = widgetSpan.child as Transform;
final Text text = transform.child as Text;
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text.data, style: text.style),
textAlign: widget.textAlign ?? TextAlign.start,
textDirection: widget.textDirection ?? TextDirection.ltr,
textScaleFactor: scale,
maxLines: maxLines,
locale: widget.locale,
strutStyle: widget.strutStyle,
)..layout();
return PlaceholderDimensions(
size: textPainter.size,
alignment: widgetSpan.alignment,
);
}).toList(),
);
}
textPainter.layout(maxWidth: constraints.maxWidth);
return !(textPainter.didExceedMaxLines || textPainter.height > constraints.maxHeight || textPainter.width > constraints.maxWidth);
}
List<WidgetSpan> _findWidgetSpans(TextSpan text) {
final List<TextSpan> textSpans = text.children?.whereType<TextSpan>().toList() ?? [];
final List<WidgetSpan> widgetSpans = text.children?.whereType<WidgetSpan>().toList() ?? [];
for (TextSpan textSpan in textSpans) {
widgetSpans.addAll(_findWidgetSpans(textSpan));
}
return widgetSpans;
} So as you can see, this solution will only work in this case since we're not working with additional boxes surrounding text or whatever. Since we only use an extra I can imagine that once you start adding paddings or things like that, it's gonna be a whole lot more complicated since you'd have to go through every child and calculate the dimensions of the end result... Not sure what would be the best way to go about that. |
Hello, I think that this is a possible way. According with previous post i add this method to auto_size_text.dart
but i've added also _findText to find the Text widget
and edit _checkTextFits with
|
I have found a bit of a hack way to remove the error. Above is better, but we simply need to add If we provide a new param Details
|
Steps to Reproduce
It seems that the package doesn't support WidgetSpan within TestSpan.
Code sample
Screenshots
Version
The text was updated successfully, but these errors were encountered: