Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make copyable Text Widget in Flutter?

When long tab on Text widget, a tooltip show up with 'copy'. When click on the 'copy' the text content should copy to system clipboard.

The following will copy the text on long tap, but does not show up 'copy', so user will not know, the content is copied to the clipboard.

class CopyableText extends StatelessWidget {   final String data;   final TextStyle style;   final TextAlign textAlign;   final TextDirection textDirection;   final bool softWrap;   final TextOverflow overflow;   final double textScaleFactor;   final int maxLines;   CopyableText(     this.data, {     this.style,     this.textAlign,     this.textDirection,     this.softWrap,     this.overflow,     this.textScaleFactor,     this.maxLines,   });   @override   Widget build(BuildContext context) {     return new GestureDetector(       child: new Text(data,           style: style,           textAlign: textAlign,           textDirection: textDirection,           softWrap: softWrap,           overflow: overflow,           textScaleFactor: textScaleFactor,           maxLines: maxLines),       onLongPress: () {         Clipboard.setData(new ClipboardData(text: data));       },     );   } } 
like image 542
Kyaw Tun Avatar asked Sep 17 '17 02:09

Kyaw Tun


People also ask

How do you get copied text from Clipboard in Flutter?

In the onPressed parameter of the TextButton , call the getData method from the Clipboard class. We need to pass in the format, so in our case, use text/plain — the format when retrieving texts from the clipboard.


1 Answers

Since Flutter 1.9 you can use

SelectableText("Lorem ipsum...") 

When text is selected the "Copy" context button will appear.

enter image description here

like image 183
Frank Treacy Avatar answered Oct 03 '22 14:10

Frank Treacy