setClipboardData static method
Writes the specified params
to the clipboard asynchronously.
Map<String, String> params = {
'plainText': 'Hello, world!',
'htmlText': '<p>Hello, world!</p>',
};
Future<bool> success = ClipboardWeb.setClipboardData(params);
success.then((value) {
if (value) {
// Clipboard write operation succeeded
} else {
// Clipboard write operation failed or clipboard is unsupported
}
});
Parameters:
params
- a Map containing the plain text and HTML text content to write to the clipboard.
Returns:
Implementation
static Future<bool> setClipboardData(Map<String, String> params) async {
final Clipboard clipboard = getClipboard();
final Map<String, dynamic> representations = <String, html.Blob>{};
if (params.containsKey('plainText')) {
/// Write the plain text.
representations['text/plain'] =
html.Blob(<dynamic>[params['plainText']], 'text/plain');
}
if (params.containsKey('htmlText')) {
/// Write the HTML text.
representations['text/html'] =
html.Blob(<dynamic>[params['htmlText']], 'text/html');
}
if (representations.isNotEmpty) {
/// Write the clipboard content.
await clipboard.write(
<ClipboardItem>[ClipboardItem(js_util.jsify(representations))]);
return true;
}
/// Failed to write the clipboard content, return false.
return false;
}