captureScreen static method

Future<String?> captureScreen()

Implementation

static Future<String?> captureScreen() async {
  await requestScreenRecordingPermission();
  String directory;

  if(PlatformUtils().isWindows){
    final String documentsDirectoryPath =
        "${Platform.environment['USERPROFILE']}";
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String pkgName = packageInfo.packageName;
    directory = p.join(documentsDirectoryPath, "Documents", ".TencentCloudChat",
        pkgName, "screenshots");
  }else{
    final dic = await getApplicationSupportDirectory();
    directory = dic.path;
  }

  const uuid = Uuid();
  final fileName = 'screenshot_${uuid.v4()}.png';
  final filePath = '$directory/$fileName';
  if (Platform.isMacOS) {
    // 在macOS平台上使用screencapture工具
    final result = await Process.run(
      'screencapture',
      ['-i', '-s', '-o', filePath],
    );
    if (result.exitCode == 0) {
      return filePath;
    } else {
      return null;
    }
  } else if (Platform.isWindows) {
    // 在Windows平台上使用snippingtool工具
    final result = await Process.run(
      'snippingtool',
      ['/clip', filePath],
    );
    if (result.exitCode == 0) {
      return filePath;
    } else {
      return null;
    }
  } else {
    // 不支持的平台
    return null;
  }
}