如何在 Flutter 上创建复制到剪贴板事件?
Posted
技术标签:
【中文标题】如何在 Flutter 上创建复制到剪贴板事件?【英文标题】:How to Create Copy to Clipboard Event on Flutter? 【发布时间】:2019-07-27 20:14:22 【问题描述】:目前我想在用户设备上为“复制到剪贴板”制作事件。
当用户单击“列表视图前导 icon.content 副本”时,文本应存储在他的设备剪贴板上。
请问有人可以帮我吗?
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>";
【问题讨论】:
听起来像***.com/questions/46260055/… 【参考方案1】: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):reportFullscreenMode 不存在 InputConnection W/ManagedChannelImpl(3886):[0] 无法解析名称。 status=1 W/Adreno-ES20(3886):<229>229> 【参考方案2】:如果 Sami Kanafani 的回答由于小吃店而引发异常,请尝试这个对我有用的解决方案。
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')));
)
【讨论】:
以上是关于如何在 Flutter 上创建复制到剪贴板事件?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter,按下按钮后如何在Text Widget上粘贴数据?
flutter如何从TextWidget复制文本Flutter专题34