Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Copy to Clipboard Event on Flutter?

Currently i want to make event for "Copy to Clipboard" on user device.

When user click to "List view leading icon.content copy" then text should be store on his device clipboard.

Please can anyone help me?

Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
    final record = Record.fromSnapshot(data);

    return Padding(
      key: ValueKey(record.name),
      padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
      child: Container(
        decoration: BoxDecoration(
          border: Border.all(color: Colors.grey),
          borderRadius: BorderRadius.circular(5.0),
        ),
        child: ListTile(
          leading: Icon(Icons.content_copy),
          title: Text(record.group),
          subtitle: Text(record.name),
          // can anyone help me how to create event on onTap action.
          // When user click then text copy to clipboard on his device. 
             onTap: () {
             debugPrint ("Tapped");
          },
          ),
      ),
    );
  }
}

class Record {
  final String name;
  final String group;
  final DocumentReference reference;

  Record.fromMap(Map<String, dynamic> map, {this.reference})
      : assert(map['name'] != null),
        assert(map['group'] != null),
        name = map['name'],
        group = map['group'];

  Record.fromSnapshot(DocumentSnapshot snapshot)
      : this.fromMap(snapshot.data, reference: snapshot.reference);

  @override
  String toString() => "Record<$name:$group>";
}
like image 548
fluttertalk Avatar asked Mar 06 '19 08:03

fluttertalk


People also ask

How do I get clipboard data in flutter?

Static MethodsgetData(String format) → Future<ClipboardData?> Retrieves data from the clipboard that matches the given format. Returns a future that resolves to true iff the clipboard contains string data. Stores the given clipboard data on the clipboard.

How do you make text copyable in flutter?

A guide to making copyable text widget in the flutterWhenever the text is selected, the copy context button will appear, and it will render the output. If you are worried about not showing the copy context button, you can use SnackBar Widget. It notifies the user about the copy.

How do I transfer data to clipboard?

Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.


1 Answers

import 'package:flutter/services.dart';

inside your onTap add the following:

onTap:(){
   Clipboard.setData(new ClipboardData(text: record.name));
   Scaffold.of(context).showSnackBar(SnackBar
     (content: Text('text copied')));
}
like image 178
Sami Kanafani Avatar answered Sep 20 '22 22:09

Sami Kanafani