system_share_util.dart 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. }
  15. }
  16. static Future<void> shareQQFile(String filePath) async {
  17. if (Platform.isAndroid) {
  18. String qqPackageName = 'com.tencent.mobileqq';
  19. //检查是否安装微信
  20. bool isInstalled = await SystemShare.isInstalled(qqPackageName);
  21. if (!isInstalled) {
  22. throw SystemShareException('未安装QQ');
  23. }
  24. SystemShare.shareFile(filePath, qqPackageName, '分享到QQ');
  25. }
  26. }
  27. static Future<bool> isInstalled(String packageName) {
  28. return SystemShare.isInstalled(packageName);
  29. }
  30. }
  31. class SystemShareException implements Exception {
  32. final String message;
  33. SystemShareException(this.message);
  34. }