When style is null in the ExpandText widget the TextStyle which the TextPainter in _ExpandTextState._buildChildren is also null.
It will not have the correct style as used in the Text widget as Text widgets inherit style from an enclosing DefaultTextStyle.
This means that the overflow calculation is wrong approximate to the difference in size of the font default font size compared to the theme.
2 ways to fix this problem:
Manually call the Text.build method and use the RichText (which could be wrapped in Semantic)
final RichText richText = (child as Text).build(context) as RichText;
// TODO: could be RichText wrapped in a Semantics widget
final TextPainter textPainter = TextPainter(
text: TextSpan(
text: widget.text,
style: richText.text.style,
),
textDirection: TextDirection.ltr,
maxLines: widget.maxLength,
)..layout(maxWidth: size.maxWidth);
2) Copy the logic from Text.build which builds up the effectiveTextStyle.
final Text textWidget = child as Text;
final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
TextStyle effectiveTextStyle = textWidget.style;
if (textWidget.style == null || textWidget.style.inherit)
effectiveTextStyle = defaultTextStyle.style.merge(textWidget.style);
if (MediaQuery.boldTextOverride(context))
effectiveTextStyle = effectiveTextStyle.merge(const TextStyle(fontWeight: FontWeight.bold));
final TextPainter textPainter = TextPainter(
text: TextSpan(
text: widget.text,
style: effectiveTextStyle,
),
textDirection: TextDirection.ltr,
maxLines: widget.maxLength,
)..layout(maxWidth: size.maxWidth);
Note: I also updated the textDirection to be TextDirection.ltr, not sure why is was TextDirection.rtl
When style is null in the
ExpandText
widget theTextStyle
which theTextPainter
in_ExpandTextState._buildChildren
is also null. It will not have the correct style as used in theText
widget asText
widgets inherit style from an enclosingDefaultTextStyle
. This means that the overflow calculation is wrong approximate to the difference in size of the font default font size compared to the theme.2 ways to fix this problem:
Text.build
method and use theRichText
(which could be wrapped in Semantic)2) Copy the logic from
Text.build
which builds up the effectiveTextStyle.Note: I also updated the
textDirection
to beTextDirection.ltr
, not sure why is wasTextDirection.rtl