Copy and paste this function, the general system has its own, simple press a few keys can be completed. But sometimes we want to integrate this feature into the app ourselves, or we want to let the copied text appear in our app without pasting it. To do this, you can use a few of the plugins I introduced today.

screen_capturer

This method is used to capture the screen. I wanted to write an article about screenshots, but I looked around and only found this one for the desktop. It was too short to write about this one, so I added it directly.

Install ๐Ÿ› 

Hit Screen_capwomen to get the latest version. The following is the latest version at the time of writing:

screen_capturer: ^ 0.1.0 from
Copy the code

Using ๐ŸฅŸ

That’s what’s expected of screencap. instance. That’s what’s expected of screencap. instance, isAccessAllowed, requestAccess, and Capture.

IsAccessAllowed and requestAccess apply only to macOS. They are used to check and apply for permission on screenshots respectively.

await ScreenCapturer.instance.requestAccess();  // Request permission
bool _isAllowed = await ScreenCapturer.instance.isAccessAllowed ();  // Check whether you have permissions
Copy the code

The purpose of our screenshot is to display the picture, so before formal screenshot, define a picture path parameter:

String _image;
Copy the code

The main method for capturing screenshots is capture, which passes two parameters:

  • String? imagePath: This property is mandatory and passes the path and name of an image
  • bool silent: Sets whether to enable the screenshot prompt tone
String _imageName = '${DateTime.now().millisecondsSinceEpoch}.png';  // Set the image name
String _imagePath = 'C:\\Users\\ilgnefz\\Pictures\\$_imageName';  // Set the path to save the image
CapturedData? _capturedData = await ScreenCapturer.instance.capture(imagePath: _imagePath);
if(_capturedData ! =null) {
  _image = _capturedData.imagePath;
  setState(() {});
} else {
  BotToast.showText(text: 'Screenshot cancelled');
}
Copy the code

Through running, it can be found that what is called here is actually the screenshot function of the system, and then the captured picture is saved. It’s a little different from Windows screenshots, which only save screenshots to the clipboard.

screen_text_extractor

Install ๐Ÿ› 

Click screen_text_Extractor to get the latest version. The following is the latest version at the time of writing:

screen_text_extractor: ^ 0.1.0 from
Copy the code

Using ๐ŸฅŸ

The main body of the plug-in is ScreenTextExtractor instance, below there are seven methods:

  1. IsAccessAllowed: Checks whether related operations are allowed on macOS only
  2. RequestAccess: Requests permission to perform related operations on macOS only
  3. ExtractFromClipboard: Extracts content from the clipboard
  4. ExtractFromScreenSelection: extracted from selection screen
  5. SimulateCtrlCKeyPress: Simulates Ctrl + C and returns a Boolean value

Women, the macOS-only methods are the same as screen_capture. that’s what’s expected of you. The next three methods will return an ExtractedData object for a Future.

Let’s first define a String object to display the retrieved content:

String _text = 'The content will be here ๐Ÿคช';
Copy the code

Gets clipboard content

ExtractedData data = await ScreenTextExtractor.instance.extractFromClipboard();
_text = data.text;
setState((){});
Copy the code

Let’s start by pressing the Windows key + V in Windows to bring up the clipboard and clear it

Take a look using this method:

We will get a blank content. For better user experience, we can add a condition.

if(data.text! .isEmpty) { BotToast.showText(text:'Clipboard has nothing ๐Ÿคจ');
} else{ _text = data.text! ; setState(() {}); }Copy the code

Let’s copy a passage now:

Then look at the results:

Success ๐Ÿ˜€, but the clipboard can store text, or can store pictures.

But ExtractedData only has a text property, so let’s see what happens:

Directly empty ๐Ÿ˜ถ

Get selection content

ExtractedData data = await ScreenTextExtractor.instance.extractFromScreenSelection(
  useAccessibilityAPIFirst: false.// Use accessibility apis, macOS only
);
if(data.text! .isEmpty) { BotToast.showText(text:'Clipboard has nothing ๐Ÿคจ');
} else{ _text = data.text! ; setState(() {}); }Copy the code

๐Ÿ‘ป if this method is on the Windows side, it returns the extractFromClipboard() method result, on the macOS side and Linux side can not be demonstrated ๐Ÿ˜ช

pasteboard

Install ๐Ÿ› 

Click on Pasteboard for the latest version. The following is the latest version at the time of writing:

pasteboard: ^ hundreds
Copy the code

Using ๐ŸฅŸ

The Pasteboard object in this plug-in has four methods in total:

  • image: Copy the image
  • file: Copies files/text
  • writeImage: Paste pictures
  • writeFile: Pastes files/text

Copy and paste text

Of course, the first step is to define a variable to store the result:

String _text = 'Haven't pasted anything yet';
Copy the code

Define a text controller that fetches input:

final TextEditingController _controller = TextEditingController();
Copy the code

Next, use pasteboard to implement copy and paste functionality:

  • Copy the text

    Void _copyText() async {if (_controller.text.isempty) {bottoast.showtext (text: 'no input, what do you want me to copy ๐Ÿฅด'); } else { final lines = const LineSplitter().convert(_controller.text); await Pasteboard.writeFiles(lines); }}Copy the code
  • Paste the text

    void _pastText() async {
      final results = await Pasteboard.files();
      if (results.isNotEmpty) {
        _text = result.toString();
        setState(() {});
      } else {
        BotToast.showText(text: 'I can't give you anything because I have baa ๐Ÿ˜ญ too'); }}Copy the code

So let’s see what happens when we don’t copy and paste. My clipboard now has a message:

Take a look at the results:

We can see that it does not read the contents of our clipboard. Try copy and paste:

As you can see from your tests, the end result is an array. Let’s see if the shear board has records:

I’m actually using the same image above, but I’m not using any screenshots since there are no changes.

From the above, we can see that the copy and paste of the Pasteboard is separated from the system.

Copy and paste

In fact, the code does not need to change, but for better display, we will change the following:

void _pastText() async {
  final results = await Pasteboard.files();
  if (results.isNotEmpty) {
    _text = ' ';
    for (final result in results) {
      _text += '$result\n';
    }
    setState(() {});
  } else {
    BotToast.showText(text: 'I can't give you anything because I have baa ๐Ÿ˜ญ too'); }}Copy the code

Here, I use the url_launcher plug-in to open the system’s file browser. The code is as follows:

void _openExplorer() async {
  const _filePath = r'C:\Users\ilgnefz\Pictures';
  final Uri _uri = Uri.file(_filePath);
  await launch(_uri.toString());
}
Copy the code

Take a look at the results:

Images are essentially files that can be copied and pasted using the method above. So I’m not going to go into the picture method

(๐Ÿคซps: in fact, I use the official example method, Base64 images for testing, found that the desired results can not be obtained. The same goes for using official examples. The method of copying the image requires passing a Uint8List parameter, which can be converted using other methods, but becomes cumbersome. I’ll do a future post on drawing images with CustomPaint that uses the method of converting images to Uint8List objects.

clipboard

Install ๐Ÿ› 

Click clipboard to get the latest version. The following is the latest version at the time of writing:

clipboard: ^ 0.1.3
Copy the code

Using ๐ŸฅŸ

The plug-in has four methods:

  • controlC: Mimics the CTTR + C key to copy
  • controlC: Imitate the CTTR + V key and paste
  • copyCopy:
  • paste: paste

Let’s look at the first two methods:

void _useCtrC() async {
  if (_controller.text.isEmpty) {
    BotToast.showText(text: 'Nothing input, what do you want me to copy ๐Ÿฅด');
  } else {
    awaitFlutterClipboard.controlC(_controller.text); }}void _useCtrV() async {
  ClipboardData result = await FlutterClipboard.controlV();
  _text = result.text.toString();
  setState(() {});
}
Copy the code

Using controlV returns a ClipboardData object.

The only difference between the last two methods is that they return a String:

void _useCopy() async {
  if (_controller.text.isEmpty) {
    BotToast.showText(text: 'Nothing input, what do you want me to copy ๐Ÿฅด');
  } else {
    awaitFlutterClipboard.copy(_controller.text); }}void _usePaste() async {
  _text = await FlutterClipboard.paste();
  setState(() {});
}
Copy the code

If we open the clipboard of the system, we can see that the above copied content is recorded. Let’s try pasting the information from the clipboard without pressing copy:

Try paste method:

๐Ÿ›ซOK, that’s the end of this article, which is for the current version of the plug-in and is not guaranteed to be applicable to future iterations of plug-in usage.

Finally, thanks to the teams leanFlutter and Mixin Network and Samuelezedi for the development and maintenance of the plugin ๐Ÿ˜. This application code has been uploaded to Github and Gitee, there is a need to download down to view learning.