system_share_util.dart 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import 'dart:io';
  2. import 'package:system_share/system_share.dart';
  3. class SystemShareUtil {
  4. SystemShareUtil._();
  5. static Future<void> shareWechatFile(String filePath) async {
  6. if (Platform.isAndroid) {
  7. String wechatPackageName = 'com.tencent.mm';
  8. //检查是否安装微信
  9. bool isInstalled = await SystemShare.isInstalled(wechatPackageName);
  10. if (!isInstalled) {
  11. throw SystemShareException('未安装微信');
  12. }
  13. SystemShare.shareFile(filePath, wechatPackageName, '分享到微信',
  14. shareFileType: 'text/*');
  15. }
  16. }
  17. static Future<void> shareQQFile(String filePath) async {
  18. if (Platform.isAndroid) {
  19. String qqPackageName = 'com.tencent.mobileqq';
  20. //检查是否安装微信
  21. bool isInstalled = await SystemShare.isInstalled(qqPackageName);
  22. if (!isInstalled) {
  23. throw SystemShareException('未安装QQ');
  24. }
  25. SystemShare.shareFile(filePath, qqPackageName, '分享到QQ',
  26. shareFileType: 'text/*');
  27. }
  28. }
  29. static Future<bool> isInstalled(String packageName) {
  30. return SystemShare.isInstalled(packageName);
  31. }
  32. }
  33. class SystemShareException implements Exception {
  34. final String message;
  35. SystemShareException(this.message);
  36. }