Skip to content

feat(share_plus): Eligible to share files with specific application via package name #3531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/share_plus/share_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ internal class MethodCallHandler(
success(isWithResult, result)
}

"shareFilesToPackage" -> {
share.shareFilesToPackage(
call.argument<List<String>>("paths")!!,
call.argument<List<String>?>("mimeTypes"),
call.argument<String?>("text"),
call.argument<String?>("subject"),
isWithResult,
call.argument<String?>("packageName"),
call.argument<List<Map<String,String>>?>("extras"),
)

success(isWithResult, result)
}

else -> result.notImplemented()
}
} catch (e: Throwable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,53 @@ internal class Share(
startActivity(chooserIntent, withResult)
}

@Throws(IOException::class)
fun shareFilesToPackage(
paths: List<String>,
mimeTypes: List<String>?,
text: String?,
subject: String?,
withResult: Boolean,
packageName: String?,
extras: List<Map<String, 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(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)
// 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) {
Expand Down
42 changes: 42 additions & 0 deletions packages/share_plus/share_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ class DemoAppState extends State<DemoApp> {
);
},
),
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) {
Expand Down Expand Up @@ -204,6 +219,33 @@ class DemoAppState extends State<DemoApp> {
});
}

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 = <XFile>[];
for (var i = 0; i < imagePaths.length; i++) {
files.add(XFile(imagePaths[i], name: imageNames[i]));
}
await Share.shareFilesToPackage(
files,
text: text,
subject: subject,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
packageName: "com.whatsapp",
extras: [
{"jid": "[email protected]"}
],
);
} else {
await Share.share(text,
subject: subject,
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
}
}

void _onShareWithResult(BuildContext context) async {
// A builder is used to retrieve the context immediately
// surrounding the ElevatedButton.
Expand Down
7 changes: 4 additions & 3 deletions packages/share_plus/share_plus/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
21 changes: 21 additions & 0 deletions packages/share_plus/share_plus/lib/share_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,25 @@ class Share {
fileNameOverrides: fileNameOverrides,
);
}

static Future<void> shareFilesToPackage(
List<XFile> files, {
String? subject,
String? text,
Rect? sharePositionOrigin,
List<String>? fileNameOverrides,
required String packageName,
List<Map<String, String>>? extras,
}) async {
assert(files.isNotEmpty);
_platform.shareFilesToPackage(
files,
subject: subject,
text: text,
sharePositionOrigin: sharePositionOrigin,
fileNameOverrides: fileNameOverrides,
packageName: packageName,
extras: extras,
);
}
}
6 changes: 3 additions & 3 deletions packages/share_plus/share_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,7 +35,8 @@
sdk: flutter
flutter_web_plugins:
sdk: flutter
share_plus_platform_interface: ^5.0.2
share_plus_platform_interface:
path: ../share_plus_platform_interface

Check warning on line 39 in packages/share_plus/share_plus/pubspec.yaml

View workflow job for this annotation

GitHub Actions / Dart Analyzer

Publishable packages can't have 'path' dependencies.

Try adding a 'publish_to: none' entry to mark the package as not for publishing or remove the path dependency.
file: ">=6.1.4 <8.0.0"
url_launcher_web: ^2.3.2
url_launcher_windows: ^3.1.2
Expand All @@ -53,4 +54,3 @@
environment:
sdk: ">=3.4.0 <4.0.0"
flutter: ">=3.22.0"

Original file line number Diff line number Diff line change
@@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,56 @@ class MethodChannelShare extends SharePlatform {
return ShareResult(result, _statusFromResult(result));
}

/// Summons the platform's share sheet to share multiple files.
@override
Future<ShareResult> shareFilesToPackage(
List<XFile> files, {
String? subject,
String? text,
Rect? sharePositionOrigin,
required String packageName,
List<Map<String, String>>? extras,
List<String>? 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 = <String, dynamic>{
'paths': paths,
'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;
}
final result =
await channel.invokeMethod<String>('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.
///
/// if file doesn't contain path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,27 @@ class SharePlatform extends PlatformInterface {
fileNameOverrides: fileNameOverrides,
);
}

/// Share [XFile] objects to package.
Future<ShareResult> shareFilesToPackage(
List<XFile> files, {
String? subject,
String? text,
Rect? sharePositionOrigin,
List<String>? fileNameOverrides,
required String packageName,
List<Map<String, String>>? extras,
}) async {
return _instance.shareFilesToPackage(
files,
subject: subject,
text: text,
sharePositionOrigin: sharePositionOrigin,
fileNameOverrides: fileNameOverrides,
packageName: packageName,
extras: extras,
);
}
}

/// The result of a share to determine what action the
Expand Down
Original file line number Diff line number Diff line change
@@ -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/

Expand Down
Loading