A Flutter plugin to share content from your Flutter app via the platform's share dialog.
一个 Flutter 插件,用于通过平台的共享对话框共享 Flutter 应用程序中的内容。
Wraps the ACTION_SEND
Intent on Android and UIActivityViewController
on iOS.
在 Android 上包装 ACTION_SEND
意图,在 iOS 上包装 UIActivityViewController
。
Platform Support 平台支持
Android | iOS | MacOS | Web | Linux | Windows |
---|---|---|---|---|---|
✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
Also compatible with Windows and Linux by using "mailto" to share text via Email.
还与Windows和Linux兼容,通过使用“mailto”通过电子邮件共享文本。
Sharing files is not supported on Windows and Linux.
共享文件在 Windows 和 Linux 上不受支持。
Usage 用法
To use this plugin, add share_plus
as a dependency in your pubspec.yaml file.
要使用此插件,请在 pubspec.yaml 文件中添加 share_plus
作为依赖项。
Example 例
Import the library. 导入库。
import 'package:share_plus/share_plus.dart';
Then invoke the static share
method anywhere in your Dart code.
然后在 Dart 代码中的任何位置调用静态 share
方法。
Share.share('check out my website https://example.com');
The share
method also takes an optional subject
that will be used when sharing to email.
share
方法还采用可选的 subject
,该 subject
将在共享到电子邮件时使用。
Share.share('check out my website https://example.com', subject: 'Look what I made!');
To share one or multiple files invoke the static shareFiles
method anywhere in your Dart code. Optionally you can also pass in text
and subject
.
若要共享一个或多个文件,请在 Dart 代码中的任何位置调用静态 shareFiles
方法。(可选)您也可以传入 text
和 subject
。
Share.shareFiles(['${directory.path}/image.jpg'], text: 'Great picture'); Share.shareFiles(['${directory.path}/image1.jpg', '${directory.path}/image2.jpg']);
On web you can use SharePlus.shareXFiles()
. This uses the Web Share API if it's available. Otherwise it falls back to downloading the shared files. See Can I Use - Web Share API to understand which browsers are supported. This builds on the cross_file
package.
在网络上,您可以使用 SharePlus.shareXFiles()
。这将使用 Web 共享 API(如果可用)。否则,它将回退到下载共享文件。请参阅是否可以使用 - Web 共享 API,了解支持哪些浏览器。这基于 cross_file
包构建。
Share.shareXFiles([XFile('assets/hello.txt')], text: 'Great picture');
Check out our documentation website to learn more. Plus plugins documentation
查看我们的文档网站以了解更多信息。 加上插件文档
Known Issues 已知问题
Sharing data created with XFile.fromData 共享使用 XFile.fromData 创建的数据
When sharing data created with XFile.fromData
, the plugin will write a temporal file inside the cache directory of the app, so it can be shared.
共享使用 XFile.fromData
创建的数据时,插件会在应用的缓存目录中写入一个临时文件,以便共享。
Althouth the OS should take care of deleting those files, it is advised, that you clean up this data once in a while (e.g. on app start).
虽然操作系统应该负责删除这些文件,但建议您偶尔清理这些数据(例如在应用程序启动时)。
You can access this directory using path_provider getTemporaryDirectory.
您可以使用 path_provider getTemporaryDirectory 访问此目录。
Alternatively, don't use XFile.fromData
and instead write the data down to a File
with a path before sharing it, so you control when to delete it.
或者,不要使用 XFile.fromData
,而是在共享数据之前将数据写成带有路径的 File
,以便控制何时删除它。
Mobile platforms (Android and iOS) 移动平台(安卓和iOS)
Facebook limitations (WhatsApp, Instagram, Facebook Messenger) Facebook限制(WhatsApp,Instagram,Facebook Messenger)
Due to restrictions set up by Facebook this plugin isn't capable of sharing data reliably to Facebook related apps on Android and iOS. This includes eg. sharing text to the Facebook Messenger. If you require this functionality please check the native Facebook Sharing SDK (https://developers.facebook.com/docs/sharing) or search for other Flutter plugins implementing this SDK. More information can be found in this issue.
由于Facebook设置的限制,此插件无法可靠地将数据共享到Android和iOS上的Facebook相关应用程序。这包括例如。将文本分享到Facebook Messenger。如果您需要此功能,请查看原生 Facebook 共享 SDK ( https://developers.facebook.com/docs/sharing ) 或搜索实现此 SDK 的其他 Flutter 插件。更多信息可以在本期中找到。
iPad
share_plus
requires iPad users to provide the sharePositionOrigin
parameter.
share_plus
要求 iPad 用户提供 sharePositionOrigin
参数。
Without it, share_plus
will not work on iPads and may cause a crash or letting the UI not responding.
没有它, share_plus
将无法在 iPad 上运行,并可能导致崩溃或让 UI 无响应。
To avoid that problem, provide the sharePositionOrigin
.
若要避免此问题,请提供 sharePositionOrigin
。
For example: 例如:
// Use Builder to get the widget context Builder( builder: (BuildContext context) { return ElevatedButton( onPressed: () => _onShare(context), child: const Text('Share'), ); }, ), // _onShare method: final box = context.findRenderObject() as RenderBox?; await Share.share( text, subject: subject, sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size, );
See the main.dart
in the example
for a complete example.
有关完整示例,请参阅 example
中的 main.dart
。