auto_size_text icon indicating copy to clipboard operation
auto_size_text copied to clipboard

Adds support for 'SelectableText'

Open definitelyme opened this issue 2 years ago • 14 comments

Tested on Android, iOS & Web platforms.

definitelyme avatar Jul 15 '21 18:07 definitelyme

@leisim Is it possible to merge this and publish this feature?

TjarcoKerssens avatar Oct 13 '21 12:10 TjarcoKerssens

Hi @TjarcoKerssens, Here's a temporary workaround till it's resolved; add this to pubspec.yaml

auto_size_text:
    git:
      url: https://github.com/definitelyme/auto_size_text.git
      ref: ft/selectable-text

definitelyme avatar Oct 16 '21 21:10 definitelyme

Can't you just use textBuilder for this?

esDotDev avatar Nov 04 '21 14:11 esDotDev

What widget is that? There isn't a TextBuilder widget in Flutter

definitelyme avatar Nov 07 '21 17:11 definitelyme

@definitelyme Thanks for this feature, I needed it in a project of mine. However I'm experiencing some weird behavior in the text wrapping.

See this example: Bildschirmfoto 2022-02-21 um 14 10 23

These are two AutoSizeText widgets in a Row, with exactly the same properties and width. The left one is the selectable version, the right side the normal version. As you see the selectable version is overflowing with the same calculated font size.

I found this issue that explains that there is a tiny extra margin with the EditableText which is used by SelectableText.

In order to fix this and correctly calculate a fitting font size, we need to account for this margin in the _checkTextFits method of _AutoSizeTextState. We need to change the textPainter.layout() call to:

if (widget._isSelectableText) {
  textPainter.layout(maxWidth: constraints.maxWidth - widget.cursorWidth - 1.0);
} else {
   textPainter.layout(maxWidth: constraints.maxWidth);
}

I tested this and it seems to work.

schultek avatar Feb 21 '22 13:02 schultek

Nice! I didn't notice this, I'll update my PR.

definitelyme avatar Feb 21 '22 14:02 definitelyme

@definitelyme Any update on this?

schultek avatar Mar 03 '22 11:03 schultek

For me isnt working well enought. Before text will shrink SelectableText appears with Scrollbar example below: image and after shrinking web container a little bit more, scroll dissapear and text is shrinked image That behavior doesnt exist in just AutoSizeText. Tried with NeverScrollablePhysics and ScrollConfiguration with scrollbars: false still same effect.

Any ideas?

PcolBP avatar Mar 04 '22 14:03 PcolBP

@PcolBP Is this after using the change I proposed in my comment before? If not its the same issue.

schultek avatar Mar 04 '22 21:03 schultek

@schultek Yeah, thats it. For me was enought to set just:

maxWidth - 2;

But, I've noticed some weird behavior. A text is blurry after shrinking.

Upper text is Selectable, bottom is Text. Before:

image

After: image

I've created own fork with customBuilders:

painterBuilder: (maxWidth) {
          return maxWidth - 2;
        },
widgetBuilder: (calculatedStyle, scaleFactor) {
          return SelectableText(
            text,
            style: calculatedStyle.copyWith(
              fontFeatures: const [FontFeature.proportionalFigures()], // Added to fix blurry text
            ),
            textAlign: TextAlign.justify,
            textScaleFactor: scaleFactor,
          );
        },

EDIT:

Issue with blurry text related to #81215

PcolBP avatar Mar 07 '22 08:03 PcolBP

@definitelyme Any update on this?

I have updated my PR to include the fix you suggested.

definitelyme avatar Mar 24 '22 08:03 definitelyme

Thanks for this branch! I was going to use it, but then unfortunately overflowReplacement is not defined in AutoSizeText.selectable. Any reason for leaving it out?

amejiarosario avatar Jul 05 '22 21:07 amejiarosario

@amejiarosario SelectableText does not define any overflowReplacement property.

definitelyme avatar Jul 13 '22 10:07 definitelyme

Since Flutter 3.3 you can use the SelectionArea widget. See https://github.com/leisim/auto_size_text/issues/49#issuecomment-1272502090 for more details.

nilsreichardt avatar Oct 09 '22 09:10 nilsreichardt