Skip to content
Open
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
34 changes: 34 additions & 0 deletions packages/share_plus/share_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,40 @@ ShareParams(
)
```

#### Preview Thumbnail

Sets a preview thumbnail shown in the share UI when sharing `text` or `uri`.

- On **Android**, rendered by the system Sharesheet (API 29+). It is ignored
for file shares, where the system builds its own preview from the files.
- On **Windows**, set as the `DataPackage` thumbnail.
- Ignored on other platforms.

> **Important:** the `XFile` **must carry a correct image MIME type**, or the
> platform treats it as a generic binary file and shows **no preview** (the
> share itself still succeeds). The plugin does **not** infer the type from the
> file contents — it is the caller's responsibility to set it. This is the most
> common reason a thumbnail does not appear.
Provide the MIME type via `XFile.mimeType`, or via a file name/path that ends in
a matching image extension. An `XFile.fromData(bytes)` created **without** a
`mimeType` falls back to `application/octet-stream` and will not render a preview.

```dart
// From in-memory bytes — you MUST pass mimeType (set it to the actual image
// type, e.g. image/jpeg or image/webp):
ShareParams(
text: 'Check this out',
previewThumbnail: XFile.fromData(bytes, mimeType: 'image/png'),
)
// From a file path — make sure the path/name has an image extension:
ShareParams(
text: 'Check this out',
previewThumbnail: XFile('/path/to/thumbnail.png'),
)
```

## Known Issues

### Sharing data created with XFile.fromData
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,45 @@ import android.os.Build
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel

/** Handles the method calls for the plugin. */
/** Handles the method calls for the plugin. */
internal class MethodCallHandler(
private val share: Share,
private val manager: ShareSuccessManager,
) : MethodChannel.MethodCallHandler {

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
expectMapArguments(call)

// We don't attempt to return a result if the current API version doesn't support it
val isWithResult =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1

if (isWithResult)
manager.setCallback(result)

try {
when (call.method) {
"share" -> {
share.share(
arguments = call.arguments<Map<String, Any>>()!!,
withResult = isWithResult,
)
success(isWithResult, result)
}
else -> result.notImplemented()
}
} catch (e: Throwable) {
manager.clear()
result.error("Share failed", e.message, e)
}
}
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
expectMapArguments(call)

// We don't attempt to return a result if the current API version doesn't support it
val isWithResult = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1

private fun success(
isWithResult: Boolean,
result: MethodChannel.Result
) {
if (!isWithResult) {
result.success("dev.fluttercommunity.plus/share/unavailable")
if (isWithResult) manager.setCallback(result)

try {
when (call.method) {
"share" -> {
share.share(
arguments = call.arguments<Map<String, Any>>()!!,
withResult = isWithResult,
)
success(isWithResult, result)
}
else -> result.notImplemented()
}
} catch (e: Throwable) {
manager.clear()
result.error("Share failed", e.message, e)
}
}

@Throws(IllegalArgumentException::class)
private fun expectMapArguments(call: MethodCall) {
require(call.arguments is Map<*, *>) { "Map arguments expected" }
private fun success(isWithResult: Boolean, result: MethodChannel.Result) {
if (!isWithResult) {
result.success("dev.fluttercommunity.plus/share/unavailable")
}
}

@Throws(IllegalArgumentException::class)
private fun expectMapArguments(call: MethodCall) {
require(call.arguments is Map<*, *>) { "Map arguments expected" }
}
}
Loading
Loading