如何在Flutter上创建复制到剪贴板事件?

7

目前我想在用户设备上创建“复制到剪贴板”的事件。

当用户点击“列表视图领先图标.内容复制”时,文本应存储在他的设备剪贴板中。

请问有谁能帮我吗?

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

听起来像 https://dev59.com/I1YO5IYBdhLWcg3wKOft - Günter Zöchbauer
2个回答

10
import 'package:flutter/services.dart';

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

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

感谢您提供的解决方案。您能否帮我,下一步要做什么来消除下面所有的错误? V/BoostFramework(3886):BoostFramework():mPerf = com.qualcomm.qti.Performance@f62a16a W/IInputConnectionWrapper(3886):在不存在的InputConnection上报告全屏模式 W/ManagedChannelImpl(3886):[{0}]无法解析名称。 状态={1} W/Adreno-ES20(3886):<get_gpu_clk:229>:打开失败:errno 13 - fluttertalk
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): 已初始化EGL,版本1.4 D/OpenGLRenderer( 3886): 交换行为1 - fluttertalk

2
如果Sami Kanafani的回答由于snackbar而引发异常,请尝试这个对我有效的解决方案:"最初的回答"。
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')));
}
)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接