From 43e3d66f7c9d6aaf513a1b3d92f37065b0797236 Mon Sep 17 00:00:00 2001 From: santoso Date: Mon, 18 Sep 2023 21:08:32 +0700 Subject: [PATCH 1/7] share whatsapp --- .../plus/share/MethodCallHandler.kt | 26 ++++++++ .../dev/fluttercommunity/plus/share/Share.kt | 46 ++++++++++++++ .../share_plus/example/lib/main.dart | 39 ++++++++++++ .../share_plus/example/pubspec.yaml | 3 +- .../share_plus/share_plus/lib/share_plus.dart | 17 ++++++ packages/share_plus/share_plus/pubspec.yaml | 6 +- .../method_channel/method_channel_share.dart | 61 +++++++++++++++++++ .../share_plus_platform.dart | 17 ++++++ 8 files changed, 213 insertions(+), 2 deletions(-) diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt index 8329b826e9..d974d4d353 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt @@ -73,6 +73,32 @@ internal class MethodCallHandler( result.error("Share failed", e.message, null) } } + "shareWhatsappFiles" -> { + expectMapArguments(call) + if (isWithResult && !manager.setCallback(result)) return + + // Android does not support showing the share sheet at a particular point on screen. + try { + share.shareWhatsappFiles( + call.argument>("paths")!!, + call.argument?>("mimeTypes"), + call.argument("text"), + call.argument("subject"), + isWithResult, + call.argument("phone"), + ) + + if (!isWithResult) { + if (isResultRequested) { + result.success("dev.fluttercommunity.plus/share/unavailable") + } else { + result.success(null) + } + } + } catch (e: IOException) { + result.error("Share failed", e.message, null) + } + } else -> result.notImplemented() } } diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt index 8b01109e30..9ecaa20e05 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt @@ -152,6 +152,52 @@ internal class Share( startActivity(chooserIntent, withResult) } + @Throws(IOException::class) + fun shareWhatsappFiles( + paths: List, + mimeTypes: List?, + text: String?, + subject: String?, + withResult: Boolean, + phone: String? + ) { + clearShareCacheFolder() + val fileUris = getUrisForPaths(paths) + val shareIntent = Intent() + when { + (fileUris.isEmpty() && !text.isNullOrBlank()) -> { + share(text, subject, withResult) + return + } + fileUris.size == 1 -> { + val mimeType = if (!mimeTypes.isNullOrEmpty()) { + mimeTypes.first() + } else { + "*/*" + } + shareIntent.apply { + action = Intent.ACTION_SEND + type = mimeType + putExtra(Intent.EXTRA_STREAM, fileUris.first()) + } + } + else -> { + shareIntent.apply { + action = Intent.ACTION_SEND_MULTIPLE + type = reduceMimeTypes(mimeTypes) + putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris) + } + } + } + shareIntent.setPackage("com.whatsapp"); + shareIntent.putExtra("jid", "$phone@s.whatsapp.net"); + if (text != null) shareIntent.putExtra(Intent.EXTRA_TEXT, text) + if (subject != null) shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject) + shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + // If we dont want the result we use the old 'createChooser' + startActivity(shareIntent, withResult) + } + private fun startActivity(intent: Intent, withResult: Boolean) { if (activity != null) { if (withResult) { diff --git a/packages/share_plus/share_plus/example/lib/main.dart b/packages/share_plus/share_plus/example/lib/main.dart index c4a256ac4d..50e88cd030 100644 --- a/packages/share_plus/share_plus/example/lib/main.dart +++ b/packages/share_plus/share_plus/example/lib/main.dart @@ -142,6 +142,21 @@ class DemoAppState extends State { ); }, ), + const SizedBox(height: 32), + Builder( + builder: (BuildContext context) { + return ElevatedButton( + style: ElevatedButton.styleFrom( + foregroundColor: Theme.of(context).colorScheme.onPrimary, + backgroundColor: Theme.of(context).colorScheme.primary, + ), + onPressed: text.isEmpty && imagePaths.isEmpty && uri.isEmpty + ? null + : () => _onShareWhatsapp(context), + child: const Text('Share Whatsapp'), + ); + }, + ), const SizedBox(height: 16), Builder( builder: (BuildContext context) { @@ -186,6 +201,30 @@ class DemoAppState extends State { }); } + void _onShareWhatsapp(BuildContext context) async { + final box = context.findRenderObject() as RenderBox?; + + if (uri.isNotEmpty) { + await Share.shareUri(Uri.parse(uri)); + } else if (imagePaths.isNotEmpty) { + final files = []; + for (var i = 0; i < imagePaths.length; i++) { + files.add(XFile(imagePaths[i], name: imageNames[i])); + } + await Share.shareWhatsappXFiles( + files, + text: text, + subject: subject, + sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, + phone: "6281555666333", + ); + } else { + await Share.share(text, + subject: subject, + sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size); + } + } + void _onShare(BuildContext context) async { // A builder is used to retrieve the context immediately // surrounding the ElevatedButton. diff --git a/packages/share_plus/share_plus/example/pubspec.yaml b/packages/share_plus/share_plus/example/pubspec.yaml index 1c0189ee05..2958757451 100644 --- a/packages/share_plus/share_plus/example/pubspec.yaml +++ b/packages/share_plus/share_plus/example/pubspec.yaml @@ -4,7 +4,8 @@ description: Demonstrates how to use the share_plus plugin. dependencies: flutter: sdk: flutter - share_plus: ^7.1.0 + share_plus: + path: ../. image_picker: ^1.0.0 file_selector: ^1.0.0 diff --git a/packages/share_plus/share_plus/lib/share_plus.dart b/packages/share_plus/share_plus/lib/share_plus.dart index 44d9e78dd3..a442ebfce6 100644 --- a/packages/share_plus/share_plus/lib/share_plus.dart +++ b/packages/share_plus/share_plus/lib/share_plus.dart @@ -202,4 +202,21 @@ class Share { sharePositionOrigin: sharePositionOrigin, ); } + + static Future shareWhatsappXFiles( + List files, { + String? subject, + String? text, + Rect? sharePositionOrigin, + String? phone, + }) async { + assert(files.isNotEmpty); + return _platform.shareWhatsappXFiles( + files, + subject: subject, + text: text, + sharePositionOrigin: sharePositionOrigin, + phone: phone, + ); + } } diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index 44c17ef024..32c5027b42 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -32,7 +32,11 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter - share_plus_platform_interface: ^3.3.0 + share_plus_platform_interface: + git: + url: https://github.com/hadiisantoso/plus_plugins.git + ref: main + path: packages/share_plus/share_plus_platform_interface file: ">=6.1.4 <8.0.0" url_launcher_web: ^2.0.16 url_launcher_windows: ^3.0.6 diff --git a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart index fe77312da8..b441bd7dc6 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart @@ -143,6 +143,42 @@ class MethodChannelShare extends SharePlatform { return ShareResult(result, _statusFromResult(result)); } + /// Summons the platform's share sheet to share multiple files and returns the result. + @override + Future shareWhatsappFilesWithResult( + List paths, { + List? mimeTypes, + String? subject, + String? text, + Rect? sharePositionOrigin, + String? phone, + }) async { + assert(paths.isNotEmpty); + assert(paths.every((element) => element.isNotEmpty)); + final params = { + 'paths': paths, + 'mimeTypes': mimeTypes ?? + paths.map((String path) => _mimeTypeForPath(path)).toList(), + }; + + if (subject != null) params['subject'] = subject; + if (text != null) params['text'] = text; + if (phone != null) params['phone'] = phone; + + if (sharePositionOrigin != null) { + params['originX'] = sharePositionOrigin.left; + params['originY'] = sharePositionOrigin.top; + params['originWidth'] = sharePositionOrigin.width; + params['originHeight'] = sharePositionOrigin.height; + } + + final result = + await channel.invokeMethod('shareWhatsappFiles', params) ?? + 'dev.fluttercommunity.plus/share/unavailable'; + + return ShareResult(result, _statusFromResult(result)); + } + /// Summons the platform's share sheet to share multiple files. @override Future shareXFiles( @@ -166,6 +202,31 @@ class MethodChannelShare extends SharePlatform { ); } + /// Summons the platform's share sheet to share multiple files. + @override + Future shareWhatsappXFiles( + List files, { + String? subject, + String? text, + Rect? sharePositionOrigin, + String? phone, + }) async { + final filesWithPath = await _getFiles(files); + + final mimeTypes = filesWithPath + .map((e) => e.mimeType ?? _mimeTypeForPath(e.path)) + .toList(); + + return shareWhatsappFilesWithResult( + filesWithPath.map((e) => e.path).toList(), + mimeTypes: mimeTypes, + subject: subject, + text: text, + sharePositionOrigin: sharePositionOrigin, + phone: phone, + ); + } + /// if file doesn't contain path /// then make new file in TemporaryDirectory and return with path /// diff --git a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart index efc7d1851f..40b40ff4cf 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart @@ -113,6 +113,23 @@ class SharePlatform extends PlatformInterface { sharePositionOrigin: sharePositionOrigin, ); } + + /// Share [XFile] objects whatsapp with Result. + Future shareWhatsappXFiles( + List files, { + String? subject, + String? text, + Rect? sharePositionOrigin, + String? phone, + }) async { + return _instance.shareWhatsappXFiles( + files, + subject: subject, + text: text, + sharePositionOrigin: sharePositionOrigin, + phone: phone, + ); + } } /// The result of a share to determine what action the From d4a20123a9481432331d990547bf734a2ffb9eda Mon Sep 17 00:00:00 2001 From: santoso Date: Tue, 19 Sep 2023 09:47:29 +0700 Subject: [PATCH 2/7] change ref --- packages/share_plus/share_plus/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index 32c5027b42..e269062dd6 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -35,7 +35,7 @@ dependencies: share_plus_platform_interface: git: url: https://github.com/hadiisantoso/plus_plugins.git - ref: main + ref: whatsapp_share path: packages/share_plus/share_plus_platform_interface file: ">=6.1.4 <8.0.0" url_launcher_web: ^2.0.16 From 400b266cb7efabd0d435e67235d9784739f9c044 Mon Sep 17 00:00:00 2001 From: santoso Date: Tue, 19 Sep 2023 11:29:21 +0700 Subject: [PATCH 3/7] share to package --- .../plus/share/MethodCallHandler.kt | 7 +- .../dev/fluttercommunity/plus/share/Share.kt | 9 +-- .../share_plus/example/lib/main.dart | 7 +- .../share_plus/share_plus/lib/share_plus.dart | 10 +-- .../method_channel/method_channel_share.dart | 66 ++++++------------- .../share_plus_platform.dart | 12 ++-- 6 files changed, 47 insertions(+), 64 deletions(-) diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt index d974d4d353..86b7ff86e8 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt @@ -73,19 +73,20 @@ internal class MethodCallHandler( result.error("Share failed", e.message, null) } } - "shareWhatsappFiles" -> { + "shareFilesToPackage" -> { expectMapArguments(call) if (isWithResult && !manager.setCallback(result)) return // Android does not support showing the share sheet at a particular point on screen. try { - share.shareWhatsappFiles( + share.shareFilesToPackage( call.argument>("paths")!!, call.argument?>("mimeTypes"), call.argument("text"), call.argument("subject"), isWithResult, - call.argument("phone"), + call.argument("packageName"), + call.argument>?>("extras"), ) if (!isWithResult) { diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt index 9ecaa20e05..f6f90d4acd 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt @@ -153,13 +153,14 @@ internal class Share( } @Throws(IOException::class) - fun shareWhatsappFiles( + fun shareFilesToPackage( paths: List, mimeTypes: List?, text: String?, subject: String?, withResult: Boolean, - phone: String? + packageName: String?, + extras: List>? ) { clearShareCacheFolder() val fileUris = getUrisForPaths(paths) @@ -189,8 +190,8 @@ internal class Share( } } } - shareIntent.setPackage("com.whatsapp"); - shareIntent.putExtra("jid", "$phone@s.whatsapp.net"); + shareIntent.setPackage(packageName); + extras?.forEach { item -> shareIntent.putExtra(item.keys.first(), item.values.first()) } if (text != null) shareIntent.putExtra(Intent.EXTRA_TEXT, text) if (subject != null) shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject) shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) diff --git a/packages/share_plus/share_plus/example/lib/main.dart b/packages/share_plus/share_plus/example/lib/main.dart index 50e88cd030..4ce1ad8d6b 100644 --- a/packages/share_plus/share_plus/example/lib/main.dart +++ b/packages/share_plus/share_plus/example/lib/main.dart @@ -211,12 +211,15 @@ class DemoAppState extends State { for (var i = 0; i < imagePaths.length; i++) { files.add(XFile(imagePaths[i], name: imageNames[i])); } - await Share.shareWhatsappXFiles( + await Share.shareFilesToPackage( files, text: text, subject: subject, sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, - phone: "6281555666333", + packageName: "com.whatsapp", + extras: [ + {"jid": "628111555333@s.whatsapp.net"} + ], ); } else { await Share.share(text, diff --git a/packages/share_plus/share_plus/lib/share_plus.dart b/packages/share_plus/share_plus/lib/share_plus.dart index a442ebfce6..8010fc2f6b 100644 --- a/packages/share_plus/share_plus/lib/share_plus.dart +++ b/packages/share_plus/share_plus/lib/share_plus.dart @@ -203,20 +203,22 @@ class Share { ); } - static Future shareWhatsappXFiles( + static Future shareFilesToPackage( List files, { String? subject, String? text, Rect? sharePositionOrigin, - String? phone, + required String packageName, + List>? extras, }) async { assert(files.isNotEmpty); - return _platform.shareWhatsappXFiles( + _platform.shareFilesToPackage( files, subject: subject, text: text, sharePositionOrigin: sharePositionOrigin, - phone: phone, + packageName: packageName, + extras: extras, ); } } diff --git a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart index b441bd7dc6..69b1568480 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart @@ -143,42 +143,6 @@ class MethodChannelShare extends SharePlatform { return ShareResult(result, _statusFromResult(result)); } - /// Summons the platform's share sheet to share multiple files and returns the result. - @override - Future shareWhatsappFilesWithResult( - List paths, { - List? mimeTypes, - String? subject, - String? text, - Rect? sharePositionOrigin, - String? phone, - }) async { - assert(paths.isNotEmpty); - assert(paths.every((element) => element.isNotEmpty)); - final params = { - 'paths': paths, - 'mimeTypes': mimeTypes ?? - paths.map((String path) => _mimeTypeForPath(path)).toList(), - }; - - if (subject != null) params['subject'] = subject; - if (text != null) params['text'] = text; - if (phone != null) params['phone'] = phone; - - if (sharePositionOrigin != null) { - params['originX'] = sharePositionOrigin.left; - params['originY'] = sharePositionOrigin.top; - params['originWidth'] = sharePositionOrigin.width; - params['originHeight'] = sharePositionOrigin.height; - } - - final result = - await channel.invokeMethod('shareWhatsappFiles', params) ?? - 'dev.fluttercommunity.plus/share/unavailable'; - - return ShareResult(result, _statusFromResult(result)); - } - /// Summons the platform's share sheet to share multiple files. @override Future shareXFiles( @@ -204,12 +168,13 @@ class MethodChannelShare extends SharePlatform { /// Summons the platform's share sheet to share multiple files. @override - Future shareWhatsappXFiles( + Future shareFilesToPackage( List files, { String? subject, String? text, Rect? sharePositionOrigin, - String? phone, + required String packageName, + List>? extras, }) async { final filesWithPath = await _getFiles(files); @@ -217,14 +182,23 @@ class MethodChannelShare extends SharePlatform { .map((e) => e.mimeType ?? _mimeTypeForPath(e.path)) .toList(); - return shareWhatsappFilesWithResult( - filesWithPath.map((e) => e.path).toList(), - mimeTypes: mimeTypes, - subject: subject, - text: text, - sharePositionOrigin: sharePositionOrigin, - phone: phone, - ); + final params = { + 'paths': filesWithPath.map((e) => e.path).toList(), + 'mimeTypes': mimeTypes, + }; + + if (subject != null) params['subject'] = subject; + if (text != null) params['text'] = text; + if (extras != null) params['extras'] = extras; + params['packageName'] = packageName; + + if (sharePositionOrigin != null) { + params['originX'] = sharePositionOrigin.left; + params['originY'] = sharePositionOrigin.top; + params['originWidth'] = sharePositionOrigin.width; + params['originHeight'] = sharePositionOrigin.height; + } + await channel.invokeMethod('shareFilesToPackage', params); } /// if file doesn't contain path diff --git a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart index 40b40ff4cf..4402ec5f21 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart @@ -114,20 +114,22 @@ class SharePlatform extends PlatformInterface { ); } - /// Share [XFile] objects whatsapp with Result. - Future shareWhatsappXFiles( + /// Share [XFile] objects to package. + Future shareFilesToPackage( List files, { String? subject, String? text, Rect? sharePositionOrigin, - String? phone, + required String packageName, + List>? extras, }) async { - return _instance.shareWhatsappXFiles( + return _instance.shareFilesToPackage( files, subject: subject, text: text, sharePositionOrigin: sharePositionOrigin, - phone: phone, + packageName: packageName, + extras: extras, ); } } From f1fe4e893a4ada6ebf5d99fe87c386e4983ed9fd Mon Sep 17 00:00:00 2001 From: santoso Date: Tue, 19 Sep 2023 11:44:57 +0700 Subject: [PATCH 4/7] interface --- packages/share_plus/share_plus/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index e269062dd6..e3230c94ed 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -35,7 +35,7 @@ dependencies: share_plus_platform_interface: git: url: https://github.com/hadiisantoso/plus_plugins.git - ref: whatsapp_share + ref: share_to_package path: packages/share_plus/share_plus_platform_interface file: ">=6.1.4 <8.0.0" url_launcher_web: ^2.0.16 From 2b498f839c300d509059bdb6e87922901b0613bd Mon Sep 17 00:00:00 2001 From: santoso Date: Tue, 19 Sep 2023 14:59:30 +0700 Subject: [PATCH 5/7] interface to internal pub dev --- packages/share_plus/share_plus/pubspec.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index e3230c94ed..ef49366ecf 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -33,10 +33,10 @@ dependencies: flutter_web_plugins: sdk: flutter share_plus_platform_interface: - git: - url: https://github.com/hadiisantoso/plus_plugins.git - ref: share_to_package - path: packages/share_plus/share_plus_platform_interface + hosted: + name: share_plus_platform_interface + url: http://pub.dev.internal + version: ^3.3.0 file: ">=6.1.4 <8.0.0" url_launcher_web: ^2.0.16 url_launcher_windows: ^3.0.6 From 6319b26095d1d4ce521adc53b882edd9509d8a89 Mon Sep 17 00:00:00 2001 From: Chandra Abdul Fattah Date: Thu, 27 Mar 2025 15:12:14 +0700 Subject: [PATCH 6/7] Eligible directly share to specific package name --- .../plus/share/MethodCallHandler.kt | 34 +++++------------ .../share_plus/example/pubspec.yaml | 7 ++-- .../share_plus/share_plus/lib/share_plus.dart | 2 + packages/share_plus/share_plus/pubspec.yaml | 4 +- .../CHANGELOG.md | 3 ++ .../method_channel/method_channel_share.dart | 37 +++++++++++++------ .../share_plus_platform.dart | 4 +- .../pubspec.yaml | 2 +- 8 files changed, 51 insertions(+), 42 deletions(-) diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt index 5f8d8f436e..7ecab7d319 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt @@ -54,31 +54,17 @@ internal class MethodCallHandler( } "shareFilesToPackage" -> { - expectMapArguments(call) - if (isWithResult && !manager.setCallback(result)) return - - // Android does not support showing the share sheet at a particular point on screen. - try { - share.shareFilesToPackage( - call.argument>("paths")!!, - call.argument?>("mimeTypes"), - call.argument("text"), - call.argument("subject"), - isWithResult, - call.argument("packageName"), - call.argument>?>("extras"), - ) + share.shareFilesToPackage( + call.argument>("paths")!!, + call.argument?>("mimeTypes"), + call.argument("text"), + call.argument("subject"), + isWithResult, + call.argument("packageName"), + call.argument>?>("extras"), + ) - if (!isWithResult) { - if (isResultRequested) { - result.success("dev.fluttercommunity.plus/share/unavailable") - } else { - result.success(null) - } - } - } catch (e: IOException) { - result.error("Share failed", e.message, null) - } + success(isWithResult, result) } else -> result.notImplemented() diff --git a/packages/share_plus/share_plus/example/pubspec.yaml b/packages/share_plus/share_plus/example/pubspec.yaml index da086b16fb..eb70c60029 100644 --- a/packages/share_plus/share_plus/example/pubspec.yaml +++ b/packages/share_plus/share_plus/example/pubspec.yaml @@ -4,7 +4,8 @@ description: Demonstrates how to use the share_plus plugin. dependencies: flutter: sdk: flutter - share_plus: ^10.1.4 + share_plus: + path: ../ image_picker: ^1.1.2 file_selector: ^1.0.3 @@ -24,5 +25,5 @@ flutter: - assets/flutter_logo.png environment: - sdk: '>=3.4.0 <4.0.0' - flutter: '>=3.22.0' + sdk: ">=3.4.0 <4.0.0" + flutter: ">=3.22.0" diff --git a/packages/share_plus/share_plus/lib/share_plus.dart b/packages/share_plus/share_plus/lib/share_plus.dart index 61a14617ff..8cdcfc27f8 100644 --- a/packages/share_plus/share_plus/lib/share_plus.dart +++ b/packages/share_plus/share_plus/lib/share_plus.dart @@ -145,6 +145,7 @@ class Share { String? subject, String? text, Rect? sharePositionOrigin, + List? fileNameOverrides, required String packageName, List>? extras, }) async { @@ -154,6 +155,7 @@ class Share { subject: subject, text: text, sharePositionOrigin: sharePositionOrigin, + fileNameOverrides: fileNameOverrides, packageName: packageName, extras: extras, ); diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index 716edb03ed..abff0b2e39 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -35,7 +35,8 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter - share_plus_platform_interface: ^5.0.2 + share_plus_platform_interface: + path: ../share_plus_platform_interface file: ">=6.1.4 <8.0.0" url_launcher_web: ^2.3.2 url_launcher_windows: ^3.1.2 @@ -53,4 +54,3 @@ dev_dependencies: environment: sdk: ">=3.4.0 <4.0.0" flutter: ">=3.22.0" - diff --git a/packages/share_plus/share_plus_platform_interface/CHANGELOG.md b/packages/share_plus/share_plus_platform_interface/CHANGELOG.md index 5e0683705c..dfeb3467eb 100644 --- a/packages/share_plus/share_plus_platform_interface/CHANGELOG.md +++ b/packages/share_plus/share_plus_platform_interface/CHANGELOG.md @@ -1,3 +1,6 @@ +## 5.1.0 + - **FEAT**(share_plus): shareFilesToPackage, allow user to share files to specific packages name + ## 5.0.2 - **REFACTOR**(all): Use range of flutter_lints for broader compatibility ([#3371](https://github.com/fluttercommunity/plus_plugins/issues/3371)). ([8a303add](https://github.com/fluttercommunity/plus_plugins/commit/8a303add3dee1acb8bac5838246490ed8a0fe408)) diff --git a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart index 69ea48146f..04d363c915 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart @@ -117,22 +117,33 @@ class MethodChannelShare extends SharePlatform { /// Summons the platform's share sheet to share multiple files. @override - Future shareFilesToPackage( - List files, { - String? subject, - String? text, - Rect? sharePositionOrigin, - required String packageName, - List>? extras, - }) async { - final filesWithPath = await _getFiles(files); + Future shareFilesToPackage( + List files, { + String? subject, + String? text, + Rect? sharePositionOrigin, + required String packageName, + List>? extras, + List? fileNameOverrides, + }) async { + assert(files.isNotEmpty); + assert( + fileNameOverrides == null || files.length == fileNameOverrides.length, + "fileNameOverrides list must have the same length as files list.", + ); + final filesWithPath = await _getFiles(files, fileNameOverrides); + assert(filesWithPath.every((element) => element.path.isNotEmpty)); final mimeTypes = filesWithPath .map((e) => e.mimeType ?? _mimeTypeForPath(e.path)) .toList(); + final paths = filesWithPath.map((e) => e.path).toList(); + assert(paths.length == mimeTypes.length); + assert(mimeTypes.every((element) => element.isNotEmpty)); + final params = { - 'paths': filesWithPath.map((e) => e.path).toList(), + 'paths': paths, 'mimeTypes': mimeTypes, }; @@ -147,7 +158,11 @@ class MethodChannelShare extends SharePlatform { params['originWidth'] = sharePositionOrigin.width; params['originHeight'] = sharePositionOrigin.height; } - await channel.invokeMethod('shareFilesToPackage', params); + final result = + await channel.invokeMethod('shareFilesToPackage', params) ?? + 'dev.fluttercommunity.plus/share/unavailable'; + + return ShareResult(result, _statusFromResult(result)); } /// Ensure that a file is readable from the file system. Will create file on-demand under TemporaryDiectory and return the temporary file otherwise. diff --git a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart index 23e3a70709..1f29e407e6 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart @@ -73,11 +73,12 @@ class SharePlatform extends PlatformInterface { } /// Share [XFile] objects to package. - Future shareFilesToPackage( + Future shareFilesToPackage( List files, { String? subject, String? text, Rect? sharePositionOrigin, + List? fileNameOverrides, required String packageName, List>? extras, }) async { @@ -86,6 +87,7 @@ class SharePlatform extends PlatformInterface { subject: subject, text: text, sharePositionOrigin: sharePositionOrigin, + fileNameOverrides: fileNameOverrides, packageName: packageName, extras: extras, ); diff --git a/packages/share_plus/share_plus_platform_interface/pubspec.yaml b/packages/share_plus/share_plus_platform_interface/pubspec.yaml index 75017c1cf4..7bbe362f06 100644 --- a/packages/share_plus/share_plus_platform_interface/pubspec.yaml +++ b/packages/share_plus/share_plus_platform_interface/pubspec.yaml @@ -1,6 +1,6 @@ name: share_plus_platform_interface description: A common platform interface for the share_plus plugin. -version: 5.0.2 +version: 5.1.0 homepage: https://github.com/fluttercommunity/plus_plugins repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/ From a184cc247dc2aed4d79920c93eabf70eb933a114 Mon Sep 17 00:00:00 2001 From: Chandra Abdul Fattah Date: Thu, 27 Mar 2025 15:26:12 +0700 Subject: [PATCH 7/7] Update share_plus version --- packages/share_plus/share_plus/CHANGELOG.md | 4 ++++ packages/share_plus/share_plus/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/share_plus/share_plus/CHANGELOG.md b/packages/share_plus/share_plus/CHANGELOG.md index f43fba7d56..5b8f1b0f19 100644 --- a/packages/share_plus/share_plus/CHANGELOG.md +++ b/packages/share_plus/share_plus/CHANGELOG.md @@ -1,3 +1,7 @@ +## 10.2.0 + + - **FEAT**(share_plus): Eligible user to share files via specific package name + ## 10.1.4 - **FIX**(share_plus): fallback for shareXFiles() to use download on web ([#3388](https://github.com/fluttercommunity/plus_plugins/issues/3388)). ([95a12ee3](https://github.com/fluttercommunity/plus_plugins/commit/95a12ee3982dd61de5d07005de62f81c2e99eb08)) diff --git a/packages/share_plus/share_plus/pubspec.yaml b/packages/share_plus/share_plus/pubspec.yaml index abff0b2e39..642fd879a6 100644 --- a/packages/share_plus/share_plus/pubspec.yaml +++ b/packages/share_plus/share_plus/pubspec.yaml @@ -1,6 +1,6 @@ name: share_plus description: Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityViewController on iOS. -version: 10.1.4 +version: 10.2.0 homepage: https://github.com/fluttercommunity/plus_plugins repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/share_plus/share_plus issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/share_plus