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)); }, ); } }
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.
Since Flutter 1.9 you can use
SelectableText("Lorem ipsum...")
When text is selected the "Copy" context button will appear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With