12

For beginning i am really sorry for my not perfect English ;)

I want to create a button that will copy the text value when this button is pressed. I was looking for it but I found no information. How can I create automatic copying in flutter framework?

Thank you in advance for your help.

Jakub Padło
  • 703
  • 3
  • 8
  • 16
  • 1
    Not sure exactly what you mean but maybe try Clipboard https://api.flutter.dev/flutter/services/Clipboard-class.html – Nemi Shah Jan 02 '20 at 16:32
  • Thank you, that is exacly what I mean. – Jakub Padło Jan 02 '20 at 16:35
  • Does this answer your question? [Flutter (Dart) How to add copy to clipboard on tap to a app?](https://stackoverflow.com/questions/55885433/flutter-dart-how-to-add-copy-to-clipboard-on-tap-to-a-app) – ViKi Vyas Feb 11 '22 at 08:48

2 Answers2

33

First, assign a name to your String:

String quote = "This is a very awesome quote";

Second, copy the String to the clipboard :

onPressed: (){
    Clipboard.setData(ClipboardData(text: quote));
},

To notify the user that it's done you could use a SnackBar:

onPressed: () =>
  Clipboard.setData(ClipboardData(text: inviteLink))
    .then((value) { //only if ->
       ScaffoldMessenger.of(context).showSnackBar(snackBar)); // -> show a notification
},
Nuqo
  • 3,793
  • 1
  • 25
  • 37
2

You can use this library clipboard_manager to do the actual act of storing the text in the clipboard. Then just access the text you want to copy through the TextEditingController instance.

RaisedButton(
  child: Text('Copy'),
  onPressed: () {
    ClipboardManager.copyToClipBoard(
            _textEditingController.value.text)
        .then((result) {
      final snackBar = SnackBar(
        content: Text('Copied to Clipboard'),
        action: SnackBarAction(
          label: 'Undo',
          onPressed: () {},
        ),
      );
      Scaffold.of(context).showSnackBar(snackBar);
    });
  },
),

or access the String through a variable

RaisedButton(
  child: Text('Copy'),
  onPressed: () {
    ClipboardManager.copyToClipBoard(
            _variableContainingString)
        .then((result) {
      final snackBar = SnackBar(
        content: Text('Copied to Clipboard'),
        action: SnackBarAction(
          label: 'Undo',
          onPressed: () {},
        ),
      );
      Scaffold.of(context).showSnackBar(snackBar);
    });
  },
),
Oliver Atienza
  • 631
  • 4
  • 8