在 Flutter 提供了 Clipboard 跟 ClipboardData 用来操作系统的复制粘贴。
源码如下:
/// 设置要复制到粘贴板中的内容
@immutable
class ClipboardData {
/// Creates data for the system clipboard.
const ClipboardData({ this.text });
/// Plain text variant of this clipboard data.
final String text;
}
/// Utility methods for interacting with the system's clipboard.
///对粘贴板进行操作的类
class Clipboard {
Clipboard._();
// Constants for common [getData] [format] types.
/// Plain text data format string.
///
/// Used with [getData].
static const String kTextPlain = 'text/plain';
/// Stores the given clipboard data on the clipboard.
///将ClipboardData中的内容复制的粘贴板
static Future<void> setData(ClipboardData data) async {
await SystemChannels.platform.invokeMethod<void>(
'Clipboard.setData',
<String, dynamic>{
'text': data.text,
},
);
}
/// Retrieves data from the clipboard that matches the