0

I am displaying a text in a card, I want to add long click functionality to display more info about selected word ( the word that is long clicked ). Which text widget supports it? SelectableText widget is not suitable, because it gives user ability to copy all of my text to somewhere else, I don't like it, I just want to find out which word is selected so I display it's meaning.

Currently I am using Html widget which apparently doesn't support long pressed word.

AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
  • 1
    `GestureDetector` has `onLongPressStart` property so you can get the location where you "long "pressed, now you can use `Paragraph` API to find the position in your text closest to the given location – pskink Dec 03 '19 at 07:15
  • So I use which Widget type to display text that supports Paragraph API? – AVEbrahimi Dec 03 '19 at 07:37
  • 1
    RichText widget, and use RenderParagraph api, not Paragraph as I said before – pskink Dec 03 '19 at 08:15
  • 1
    update: with `Text` it works too - `Text` uses `RenderParagraph` as its `RenderObject` as well – pskink Dec 03 '19 at 08:52
  • I am sort of newbie, how can I find which word is at Offset of a RichText – AVEbrahimi Dec 03 '19 at 09:45
  • give your `Text` widget a `key: someGlobalKey` where `GloblaKey someGlobalKey = GloblaKey();` then use `RenderParagraph renderParagraph = someGlobalKey.currentContext.findRenderObject(); ` now you have your `RenderParagraph` object – pskink Dec 03 '19 at 09:47

1 Answers1

-1

You can wrap your Text widget with GestureDetector which has onLongPressed method.

body: Center(
        child: Card(
          child: GestureDetector(
            child: Text('Long press'),
            onLongPress: () {
              // your code here
            },
          )
        )
      )

Hope this answers your question.

Darshan
  • 10,550
  • 5
  • 49
  • 61