如何在Flutter上创建“复制到剪贴板"事件? [英] How to Create Copy to Clipboard Event on Flutter?

查看:272
本文介绍了如何在Flutter上创建“复制到剪贴板"事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我想在用户设备上创建复制到剪贴板"事件.

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

当用户单击列表视图前导icon.content复制"时,则文本应存储在其设备剪贴板上.

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>";
}

推荐答案

import 'package:flutter/services.dart';

在您的onTap内添加以下内容:

inside your onTap add the following:

onTap:(){
   Clipboard.setData(new ClipboardData(text: record.name));
   Scaffold.of(context).showSnackBar(SnackBar
     (content: Text('text copied')));
}

这篇关于如何在Flutter上创建“复制到剪贴板"事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆