jesusrp98 / expand_widget

Ability to easily expand and retract a widget collection or text
https://pub.dev/packages/expand_widget
GNU General Public License v3.0
45 stars 22 forks source link

TextPainter does not use Theme to calculate overflow #11

Closed JamesMcIntosh closed 4 years ago

JamesMcIntosh commented 4 years ago

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:

  1. 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