Mr Man
Mr Man

Reputation: 67

How to copy text inside container in flutter

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

Upvotes: 0

Views: 1669

Answers (2)

Raiyan
Raiyan

Reputation: 428

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.

Upvotes: 1

Binay Kumar
Binay Kumar

Reputation: 21

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
)

Upvotes: 0

Related Questions