skip to Main Content

I want if Someone long press on ListTile, than copy the Text..

Here is snippet of my code,

Card(
                margin: EdgeInsets.symmetric(horizontal: 25.0, vertical: 10.0),
                child: ListTile(
                  leading: Icon(
                    Icons.call,
                    color: Colors.teal,
                    size: 30.0,
                  ),
                  title: Text(
                    '+11 11111 11111',
                    style: TextStyle(
                      color: Colors.black,
                      fontFamily: 'EncodeSansExpanded',
                      fontSize: 15.0,
                    ),
                  ),
                )
              ),

I want to do like, anyone long-press on it than copy the number in above code..

4

Answers


  1. you could do something like this:

    first declare the text you are trying to copy as a variable:

    String text = '+11 11111 11111';
    

    then wrap you code in a GestureDetector Widget a use the onLongPress method and us import flutter services to use Clipboard.setdata like this

    import 'package:flutter/services.dart';
    
    
    GestureDetector(
                  onLongPress: () async {
                    await Clipboard.setData(ClipboardData(text: text));  //<- copy to clipboard functionality 
                  },
                  child: Card(
                      margin:
                          EdgeInsets.symmetric(horizontal: 25.0, vertical: 10.0),
                      child: ListTile(
                        leading: Icon(
                          Icons.call,
                          color: Colors.teal,
                          size: 30.0,
                        ),
                        title: Text(
                          text, //<- your text here
                          style: TextStyle(
                            color: Colors.black,
                            fontFamily: 'EncodeSansExpanded',
                            fontSize: 15.0,
                          ),
                        ),
                      )),
                ),
    
    Login or Signup to reply.
  2. Try below answer when you longPressed on ListTile it will copy the your data. Read more about Clipboard

    Import below package:

    import 'package:flutter/services.dart';
    

    UI:

    Card(
            margin: EdgeInsets.symmetric(horizontal: 25.0, vertical: 10.0),
            child: ListTile(
              onLongPress: () async {
                await Clipboard.setData(ClipboardData(text: '+11 11111 11111'))
                    .then(
                  (_) {
                    ScaffoldMessenger.of(context).showSnackBar(
                      SnackBar(
                        content: Text('Data Copied'),
                      ),
                    );
                  },
                );
              },
              leading: Icon(
                Icons.call,
                color: Colors.teal,
                size: 30.0,
              ),
              title: Text(
                '+11 11111 11111',
                style: TextStyle(
                  color: Colors.black,
                  fontFamily: 'EncodeSansExpanded',
                  fontSize: 15.0,
                ),
              ),
            ),
          ),
    
    Login or Signup to reply.
  3. Wrap you card with "GestureDetector" then pass the text to the Clipboard.setData.

    Here is the an example.

    Card(
          margin: EdgeInsets.symmetric(horizontal: 25.0, vertical: 10.0),
          child: GestureDetector(
            onLongPress: () {
              Clipboard.setData(ClipboardData(text: phoneNumber));
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text('Phone number copied to clipboard!'),
                ),
              );
            },
            child: ListTile(
              leading: Icon(
                Icons.call,
                color: Colors.teal,
                size: 30.0,
              ),
              title: Text(
                phoneNumber,
                style: TextStyle(
                  color: Colors.black,
                  fontFamily: 'EncodeSansExpanded',
                  fontSize: 15.0,
                ),
              ),
            ),
          ),
        );
    
    Login or Signup to reply.
  4. You can embed Card in SelectionArea

    Like this:

        SelectionArea(
          child: Card(
            margin: EdgeInsets.symmetric(horizontal: 25.0, vertical: 10.0),
            child: ListTile(
              leading: Icon(
                Icons.call,
                color: Colors.teal,
                size: 30.0,
              ),
              title: Text(
                '+11 11111 11111',
                style: TextStyle(
                  color: Colors.black,
                  fontFamily: 'EncodeSansExpanded',
                  fontSize: 15.0,
                ),
              ),
            ),
          ),
        )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search