当前,我要在用户设备上创建“复制到剪贴板”事件。
当用户单击“列表视图前导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>";
}
答案 0 :(得分:0)
import 'package:flutter/services.dart';
在您的onTap
内添加以下内容:
onTap:(){
Clipboard.setData(new ClipboardData(text: record.name));
Scaffold.of(context).showSnackBar(SnackBar
(content: Text('text copied')));
}
答案 1 :(得分:-1)
如果由于小吃店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')));
}
)