copyToClipboard function

Future<bool> copyToClipboard(
  1. String text
)

Copies text to the system clipboard. Returns true on success, false on failure.

Implementation

Future<bool> copyToClipboard(String text) async {
  try {
    if (Platform.isMacOS) {
      final p = await Process.start('pbcopy', []);
      p.stdin.write(text);
      await p.stdin.close();
      await p.exitCode;
      return true;
    } else if (Platform.isLinux) {
      try {
        final p = await Process.start('xclip', ['-selection', 'clipboard']);
        p.stdin.write(text);
        await p.stdin.close();
        await p.exitCode;
        return true;
      } catch (_) {
        final p = await Process.start('xsel', ['--clipboard', '--input']);
        p.stdin.write(text);
        await p.stdin.close();
        await p.exitCode;
        return true;
      }
    } else if (Platform.isWindows) {
      final p = await Process.start('clip', []);
      p.stdin.write(text);
      await p.stdin.close();
      await p.exitCode;
      return true;
    }
  } catch (_) {}
  return false;
}