0

Please, I added a picture below. Here is a container I've created with a button, with an icon and two different texts(one up and one down). Please how do I do it that if I long press on the button the bottom text will be copied to my clipboard, so I can paste it elsewhere on my phone

enter image description here

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
Mr Man
  • 67
  • 1
  • 8
  • 1
    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) – croxx5f Aug 30 '21 at 16:39

2 Answers2

1

Import this:

import 'package:flutter/services.dart';

Wrap your button widget with GestureDetector and add an anonymous function

GestureDetector(
  onLongPress: () {
    Clipboard.setData(ClipboardData(text: yourText));
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text("Text copied to clipboard"),
      ),
    );
  },
  child: // Your button widget
)

Edit: I've added code to show a popup that says text copied to clipboard.

Raiyan
  • 388
  • 2
  • 11
  • How do I do do that there will be a small pop up that says copied to clipboard after the text is copied – Mr Man Aug 30 '21 at 18:15
0
import 'package:flutter/services.dart';

Put your container widget inside the GestureDectector and onLongPress you can add function to copy your desired text to clipboard.

GestureDetector(
  onLongPress: () {
    Clipboard.setData(ClipboardData(text: yourText));
    Toast.show('Text Copied');
  },
  child: // Your Container
)
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77