20

I need to display a styled text for reading and some actions. User should be able to select piece of text and mark it with color, save it or expand it (show an additional information, for example a translation to another language).

I can display text by using RichText widget.

1) How to make it selectable and how/where to add onTextSelected listener? There is a class TextSelection but I can't see how/where its used.

2) What is a simplest way to expand text? I can reload full text (with changes added) and totally update widget, but this will result to scrolling to the top of a text and I think there should be a better approach.

Serge Breusov
  • 1,316
  • 4
  • 11
  • 24
  • 2
    Not a perfect solution but you can look into [this](https://docs.flutter.io/flutter/rendering/RenderEditable-class.html) and [this](https://github.com/flutter/flutter/issues/5422) – Rohan Taneja Sep 21 '18 at 22:32
  • @Serge Breusov did you find any solution for this? – Siva Perumal Jun 16 '20 at 10:51
  • Does this answer your question? [How to make copyable Text Widget in Flutter?](https://stackoverflow.com/questions/46260055/how-to-make-copyable-text-widget-in-flutter) – Karan Owalekar Aug 13 '21 at 07:34

3 Answers3

12

You now have SelectableText() widget available

Sebastian
  • 3,666
  • 2
  • 19
  • 32
1

There is some progress in making text selection possible in Flutter. The only working solution at the moment is using RenderEditable with suppressed keyboard calls and removed cursor.

See flutter_selectable_text plugin to make text selectable.

To also listen for selection events - consider using flutter_selectext plugin, which also supports rich text (TextSpan).


However, I would recommend waiting for the official implementation that is supposed to arrive in the end of May 2019.

George Zvonov
  • 9,401
  • 5
  • 33
  • 37
-2

Use GestureDetector out of your text view. For example

GestureDetector(
          onTap: (){},
          child: Text(
            'Your text',
           style: TextStyle(
                    color: Colors.green,
                  ),
          ),
        ),

And as for the rest of the question I dont understand

  • You completely misunderstood the question, I believe. The OP was asking about the `selectable` or `hightlightable` and 'expand` a `Text()` when `onClicked` – 钟智强 Aug 24 '22 at 05:21