7

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>";
}
fluttertalk
  • 499
  • 1
  • 6
  • 13

2 Answers2

10
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')));
}
Sami Kanafani
  • 14,244
  • 6
  • 45
  • 41
  • Thank you for your above solution. Can you help me, what i have to do next for remove all below error? V/BoostFramework( 3886): BoostFramework() : mPerf = com.qualcomm.qti.Performance@f62a16a W/IInputConnectionWrapper( 3886): reportFullscreenMode on inexistent InputConnection W/ManagedChannelImpl( 3886): [{0}] Failed to resolve name. status={1} W/Adreno-ES20( 3886): : open failed: errno 13 – fluttertalk Mar 06 '19 at 16:33
  • W/ple.flutterfire( 3886): type=1400 audit(0.0:253): avc: denied { read } for name="gpuclk" dev="sysfs" ino=20996 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:sysfs:s0 tclass=file permissive=0 I/OpenGLRenderer( 3886): Initialized EGL, version 1.4 D/OpenGLRenderer( 3886): Swap behavior 1 – fluttertalk Mar 06 '19 at 16:33
2

If Sami Kanafani's answer throws an exception because of the snackbar try this solution that worked for me.

final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();

...//build method
Scaffold(
key: _scaffoldKey,
appBar: _appBar,
body: _content,
floatingActionButton: FloatingActionButton.extended(
label: Text('copy'),
onPressed:(){
  Clipboard.setData(new ClipboardData(text: record.name));
   _scaffoldKey.currentState.showSnackBar(SnackBar
     (content: Text('text copied')));
}
)
F-1
  • 2,887
  • 20
  • 28