Skip to content

Commit 0a19d46

Browse files
feat(share_plus)!: SharePlus refactor (#3404)
Co-authored-by: Copilot <[email protected]>
1 parent 6648753 commit 0a19d46

File tree

22 files changed

+979
-771
lines changed

22 files changed

+979
-771
lines changed

packages/share_plus/share_plus/README.md

+92-29
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ on iOS, or equivalent platform content sharing methods.
1414

1515
## Platform Support
1616

17-
| Method | Android | iOS | MacOS | Web | Linux | Windows |
18-
| :-----------: | :-----: | :-: | :---: | :-: | :---: | :----: |
19-
| `share` |||||||
20-
| `shareUri` | || | | | |
21-
| `shareXFiles` ||||| ||
17+
| Shared content | Android | iOS | MacOS | Web | Linux | Windows |
18+
| :------------: | :-----: | :-: | :---: | :-: | :---: | :-----: |
19+
| Text |||||||
20+
| URI | | | | As text | As text | As text |
21+
| Files ||||| ||
2222

2323
Also compatible with Windows and Linux by using "mailto" to share text via Email.
2424

@@ -47,23 +47,30 @@ import 'package:share_plus/share_plus.dart';
4747

4848
### Share Text
4949

50-
Invoke the static `share()` method anywhere in your Dart code.
50+
Access the `SharePlus` instance via `SharePlus.instance`.
51+
Then, invoke the `share()` method anywhere in your Dart code.
5152

5253
```dart
53-
Share.share('check out my website https://example.com');
54+
SharePlus.instance.share(
55+
ShareParams(text: 'check out my website https://example.com')
56+
);
5457
```
5558

56-
The `share` method also takes an optional `subject` that will be used when
57-
sharing to email.
59+
The `share()` method requires the `ShareParams` object,
60+
which contains the content to share.
5861

59-
```dart
60-
Share.share('check out my website https://example.com', subject: 'Look what I made!');
61-
```
62+
These are some of the accepted parameters of the `ShareParams` class:
63+
64+
- `text`: text to share.
65+
- `title`: content or share-sheet title (if supported).
66+
- `subject`: email subject (if supported).
67+
68+
Check the class documentation for more details.
6269

6370
`share()` returns `status` object that allows to check the result of user action in the share sheet.
6471

6572
```dart
66-
final result = await Share.share('check out my website https://example.com');
73+
final result = await SharePlus.instance.share(params);
6774
6875
if (result.status == ShareResultStatus.success) {
6976
print('Thank you for sharing my website!');
@@ -72,18 +79,31 @@ if (result.status == ShareResultStatus.success) {
7279

7380
### Share Files
7481

75-
To share one or multiple files, invoke the static `shareXFiles` method anywhere in your Dart code. The method returns a `ShareResult`. Optionally, you can pass `subject`, `text` and `sharePositionOrigin`.
82+
To share one or multiple files, provide the `files` list in `ShareParams`.
83+
Optionally, you can pass `title`, `text` and `sharePositionOrigin`.
7684

7785
```dart
78-
final result = await Share.shareXFiles([XFile('${directory.path}/image.jpg')], text: 'Great picture');
86+
final params = ShareParams(
87+
text: 'Great picture',
88+
files: [XFile('${directory.path}/image.jpg')],
89+
);
90+
91+
final result = await SharePlus.instance.share(params);
7992
8093
if (result.status == ShareResultStatus.success) {
8194
print('Thank you for sharing the picture!');
8295
}
8396
```
8497

8598
```dart
86-
final result = await Share.shareXFiles([XFile('${directory.path}/image1.jpg'), XFile('${directory.path}/image2.jpg')]);
99+
final params = ShareParams(
100+
files: [
101+
XFile('${directory.path}/image1.jpg'),
102+
XFile('${directory.path}/image2.jpg'),
103+
],
104+
);
105+
106+
final result = await SharePlus.instance.share(params);
87107
88108
if (result.status == ShareResultStatus.dismissed) {
89109
print('Did you not like the pictures?');
@@ -96,15 +116,13 @@ See [Can I Use - Web Share API](https://caniuse.com/web-share) to understand
96116
which browsers are supported. This builds on the [`cross_file`](https://pub.dev/packages/cross_file)
97117
package.
98118

99-
100-
```dart
101-
Share.shareXFiles([XFile('assets/hello.txt')], text: 'Great picture');
102-
```
103-
104119
File downloading fallback mechanism for web can be disabled by setting:
105120

106121
```dart
107-
Share.downloadFallbackEnabled = false;
122+
ShareParams(
123+
// rest of params
124+
downloadFallbackEnabled: false,
125+
)
108126
```
109127

110128
#### Share Data
@@ -114,7 +132,12 @@ You can also share files that you dynamically generate from its data using [`XFi
114132
To set the name of such files, use the `fileNameOverrides` parameter, otherwise the file name will be a random UUID string.
115133

116134
```dart
117-
Share.shareXFiles([XFile.fromData(utf8.encode(text), mimeType: 'text/plain')], fileNameOverrides: ['myfile.txt']);
135+
final params = ShareParams(
136+
files: [XFile.fromData(utf8.encode(text), mimeType: 'text/plain')],
137+
fileNameOverrides: ['myfile.txt']
138+
);
139+
140+
SharePlus.instance.share(params);
118141
```
119142

120143
> [!CAUTION]
@@ -123,10 +146,13 @@ Share.shareXFiles([XFile.fromData(utf8.encode(text), mimeType: 'text/plain')], f
123146
### Share URI
124147

125148
iOS supports fetching metadata from a URI when shared using `UIActivityViewController`.
126-
This special method is only properly supported on iOS.
149+
This special functionality is only properly supported on iOS.
150+
On other platforms, the URI will be shared as plain text.
127151

128152
```dart
129-
Share.shareUri(uri: uri);
153+
final params = ShareParams(uri: uri);
154+
155+
SharePlus.instance.share(params);
130156
```
131157

132158
### Share Results
@@ -201,15 +227,52 @@ Builder(
201227
// _onShare method:
202228
final box = context.findRenderObject() as RenderBox?;
203229
204-
await Share.share(
205-
text,
206-
subject: subject,
207-
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
230+
await SharePlus.instance.share(
231+
ShareParams(
232+
text: text,
233+
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
234+
)
208235
);
209236
```
210237

211238
See the `main.dart` in the `example` for a complete example.
212239

240+
## Migrating from `Share.share()` to `SharePlus.instance.share()`
241+
242+
The static methods `Share.share()`, `Share.shareUri()` and `Share.shareXFiles()`
243+
have been deprecated in favor of the `SharePlus.instance.share(params)`.
244+
245+
To convert code using `Share.share()` to the new `SharePlus` class:
246+
247+
1. Wrap the current parameters in a `ShareParams` object.
248+
2. Change the call to `SharePlus.instance.share()`.
249+
250+
e.g.
251+
252+
```dart
253+
Share.share("Shared text");
254+
255+
Share.shareUri("http://example.com");
256+
257+
Share.shareXFiles(files);
258+
```
259+
260+
Becomes:
261+
262+
```dart
263+
SharePlus.instance.share(
264+
ShareParams(text: "Shared text"),
265+
);
266+
267+
SharePlus.instance.share(
268+
ShareParams(uri: "http://example.com"),
269+
);
270+
271+
SharePlus.instance.share(
272+
ShareParams(files: files),
273+
);
274+
```
275+
213276
## Learn more
214277

215278
- [API Documentation](https://pub.dev/documentation/share_plus/latest/share_plus/share_plus-library.html)

packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt

+2-26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package dev.fluttercommunity.plus.share
22

33
import android.os.Build
4-
import io.flutter.BuildConfig
54
import io.flutter.plugin.common.MethodCall
65
import io.flutter.plugin.common.MethodChannel
7-
import java.io.IOException
86

97
/** Handles the method calls for the plugin. */
108
internal class MethodCallHandler(
@@ -24,35 +22,13 @@ internal class MethodCallHandler(
2422

2523
try {
2624
when (call.method) {
27-
"shareUri" -> {
28-
share.share(
29-
call.argument<Any>("uri") as String,
30-
subject = null,
31-
withResult = isWithResult,
32-
)
33-
success(isWithResult, result)
34-
}
35-
3625
"share" -> {
3726
share.share(
38-
call.argument<Any>("text") as String,
39-
call.argument<Any>("subject") as String?,
40-
isWithResult,
41-
)
42-
success(isWithResult, result)
43-
}
44-
45-
"shareFiles" -> {
46-
share.shareFiles(
47-
call.argument<List<String>>("paths")!!,
48-
call.argument<List<String>?>("mimeTypes"),
49-
call.argument<String?>("text"),
50-
call.argument<String?>("subject"),
51-
isWithResult,
27+
arguments = call.arguments<Map<String, Any>>()!!,
28+
withResult = isWithResult,
5229
)
5330
success(isWithResult, result)
5431
}
55-
5632
else -> result.notImplemented()
5733
}
5834
} catch (e: Throwable) {

0 commit comments

Comments
 (0)