0

This question Flutter (Dart) How to add copy to clipboard on tap to a app? already tells how to copy a text to clipboard in dart.

Now I would like to copy a file or directory to clipboard. Something like this:

void main(List<String> args) {
  String fileOrDirectoryPath = "/some/path/file.txt";
  copyFileOrDirectoryToClipboard(fileOrDirectoryPath);
}

Then I would be able to paste the file or directory in my file manager with Ctrl + V.

Any idea to achieve this ?

Lenny4
  • 1,215
  • 1
  • 14
  • 33
  • 1
    Not currently possible without writing platform specific code. The flutter [ClipboardData class](https://api.flutter.dev/flutter/services/ClipboardData-class.html): `This data structure currently supports only plain text data, in the text property.` You can write some Windows specific code to PInvoke [SetClipboardData](https://www.pinvoke.net/default.aspx/user32/SetClipboardData.html) – Wh1t3rabbit Apr 26 '23 at 04:47
  • 1
    Of course there is the possibility someone has already written that platform specific code: there is the package [super_clipboard](https://pub.dev/packages/super_clipboard) which seems to offer file support – Wh1t3rabbit Apr 26 '23 at 04:55

1 Answers1

0

You can use the super_clipboard package which supports all platforms.

Directory temp = Directory.systemTemp;
                Directory tempHome = Directory('${temp.path}/home');
                await tempHome.create();
                File file = File('${tempHome.path}/home.dart');

                await file.writeAsString('This is a test');

                final item = DataWriterItem();

                item.add(Formats.fileUri(tempHome.uri));
                await ClipboardWriter.instance.write([item]);
Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61