| 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'package:flutter/foundation.dart'; |
| 6 | |
| 7 | import 'system_channels.dart'; |
| 8 | |
| 9 | /// Data stored on the system clipboard. |
| 10 | /// |
| 11 | /// The system clipboard can contain data of various media types. This data |
| 12 | /// structure currently supports only plain text data, in the [text] property. |
| 13 | @immutable |
| 14 | class ClipboardData { |
| 15 | /// Creates data for the system clipboard. |
| 16 | const ClipboardData({required String this.text}); |
| 17 | |
| 18 | /// Plain text variant of this clipboard data. |
| 19 | // This is nullable as other clipboard data variants, like images, may be |
| 20 | // added in the future. Currently, plain text is the only supported variant |
| 21 | // and this is guaranteed to be non-null. |
| 22 | final String? text; |
| 23 | } |
| 24 | |
| 25 | /// Utility methods for interacting with the system's clipboard. |
| 26 | abstract final class Clipboard { |
| 27 | // Constants for common [getData] [format] types. |
| 28 | |
| 29 | /// Plain text data format string. |
| 30 | /// |
| 31 | /// Used with [getData]. |
| 32 | static const String kTextPlain = 'text/plain' ; |
| 33 | |
| 34 | /// Stores the given clipboard data on the clipboard. |
| 35 | static Future<void> setData(ClipboardData data) async { |
| 36 | await SystemChannels.platform.invokeMethod<void>('Clipboard.setData' , <String, dynamic>{ |
| 37 | 'text' : data.text, |
| 38 | }); |
| 39 | } |
| 40 | |
| 41 | /// Retrieves data from the clipboard that matches the given format. |
| 42 | /// |
| 43 | /// The `format` argument specifies the media type, such as `text/plain`, of |
| 44 | /// the data to obtain. |
| 45 | /// |
| 46 | /// Returns a future which completes to null if the data could not be |
| 47 | /// obtained, and to a [ClipboardData] object if it could. |
| 48 | static Future<ClipboardData?> getData(String format) async { |
| 49 | final Map<String, dynamic>? result = await SystemChannels.platform.invokeMethod( |
| 50 | 'Clipboard.getData' , |
| 51 | format, |
| 52 | ); |
| 53 | if (result == null) { |
| 54 | return null; |
| 55 | } |
| 56 | return ClipboardData(text: result['text' ] as String); |
| 57 | } |
| 58 | |
| 59 | /// Returns a future that resolves to true, if (and only if) |
| 60 | /// the clipboard contains string data. |
| 61 | /// |
| 62 | /// See also: |
| 63 | /// * [The iOS hasStrings method](https://developer.apple.com/documentation/uikit/uipasteboard/1829416-hasstrings?language=objc). |
| 64 | static Future<bool> hasStrings() async { |
| 65 | final Map<String, dynamic>? result = await SystemChannels.platform.invokeMethod( |
| 66 | 'Clipboard.hasStrings' , |
| 67 | Clipboard.kTextPlain, |
| 68 | ); |
| 69 | if (result == null) { |
| 70 | return false; |
| 71 | } |
| 72 | return result['value' ] as bool; |
| 73 | } |
| 74 | } |
| 75 | |