搜索插件、代码仓与 APP 分类

插件详情v1.0.4

akvelon_flutter_share_plugin

一个 Flutter 插件,可通过平台的共享对话框从您的 Flutter 应用分享文本、媒体和文件。

近 30 天下载
73
喜欢数
28
Pub 分数
35

安装插件

flutter pub add akvelon_flutter_share_plugin

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter 插件,可让你的应用使用平台原生的共享对话框分享文本、媒体和文件。它提供了一个简单且跨平台的接口,可将共享功能无缝集成到 Flutter 应用中。该插件利用 Android 和 iOS 上的原生共享 API,确保在各种设备上行为一致。它支持分享纯文本、图片和文件,非常适合需要用户驱动内容分发的应用。源代码公开托管在 GitHub 上,以保证透明度并支持自定义。

适合场景

  • 在应用程序内共享文本片段
  • 发送图片到社交媒体或消息应用
  • 分发文件,如PDF或文档
  • 启用用户与联系人共享内容
  • 在跨平台应用中集成原生分享功能

主要特点

  • 跨平台共享支持
  • 用于共享文本和媒体的简单 API
  • 文件共享支持
  • 使用原生平台共享对话框
  • 开源并公开托管在 GitHub 上

适合对象

  • 开发跨平台应用的开发者
  • 需要内容共享功能的应用程序
  • 需要与原生共享集成的项目
  • 希望代码透明且可定制的团队
  • 针对 Android 和 iOS 的移动应用

使用建议

  • 需要平台特定的权限才能共享文件
  • 仅限基本共享功能
  • 除了原生分享表单外,没有内置的UI组件
  • 取决于分享意图的平台可用性
  • 可能需要对大文件进行额外处理

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Flutter Share Plugin

A Flutter plugin for both iOS and Android that provides ability to share plain text, links and local files. Multiple file share is also supported.

Screenshots

Click to see how it works

Installation

In the beginning, please add flutter_share_plugin as a dependency in your pubspec.yaml file.

iOS

Make sure that this row exists in your ios/podfile file after target runner:

...

target 'Runner' do
    use_frameworks!

...
Android

Please note that if you want to share files, you need to setup a FileProvider which will give access to the files for sharing with other applications.

Add to AndroidManifest.xml:

<application>
...
<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths"/>
</provider>
</application>
...

Add res/xml/file_provider_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="cache" path="."/>
    <files-path name="cache" path="."/>
</paths>

For full document about FileProvider please refer to the Android documentation.

Usage

Here is an example of flutter app sharing text, images and files:

...

            RaisedButton(
                onPressed: () {
                  FlutterSharePlugin.shareText("Here is how it works",
                      title: "share text title",
                      subject: "share text subject",
                      url: "https://pub.dev/");
                },
                child: Text("share text"),
              ),
              RaisedButton(
                onPressed: () async {
                  PickedFile f =
                      await picker.getImage(source: ImageSource.gallery);
                  if (f != null) {
                    FlutterSharePlugin.shareSingle(f.path, ShareType.IMAGE,
                        title: "share image title",
                        subject: "share image subject");
                  }
                },
                child: Text("share image"),
              ),
              RaisedButton(
                onPressed: () async {
                  PickedFile f =
                      await picker.getVideo(source: ImageSource.gallery);
                  if (f != null) {
                    FlutterSharePlugin.shareSingle(
                      f.path,
                      ShareType.VIDEO,
                      title: "share video title",
                      subject: "share video subject",
                    );
                  }
                },
                child: Text("share video"),
              ),
              RaisedButton(
                onPressed: () {
                  _shareStorageFile();
                },
                child: Text("share file"),
              ),
              RaisedButton(
                onPressed: () {
                  _shareMultipleImages();
                },
                child: Text("share multiple images"),
              ),
            ],
          ),
        ),
      ),
    ),
  );
}

  ///share the storage file
  _shareStorageFile() async {
    Directory dir = Platform.isAndroid
        ? await getExternalStorageDirectory()
        : await getApplicationDocumentsDirectory();
    File testFile = File("${dir.path}/flutter/test.txt");
    if (!await testFile.exists()) {
      await testFile.create(recursive: true);
      testFile.writeAsStringSync("test for share documents file");
    }
    FlutterSharePlugin.shareSingle(
      testFile.path,
      ShareType.FILE,
      title: "share file title",
      subject: "share file subject",
    );
  }

  ///share multiple images
  _shareMultipleImages() async {
    List<Asset> assetList = await MultiImagePicker.pickImages(maxImages: 5);
    var imageList = List<String>();
    for (var asset in assetList) {
      String path =
          await _writeByteToImageFile(await asset.getByteData(quality: 30));
      imageList.add(path);
    }
    FlutterSharePlugin.shareMultiple(
      imageList,
      ShareType.IMAGE,
      title: "share multiple images title",
      subject: "share multiple images subject",
    );
  }

  Future<String> _writeByteToImageFile(ByteData byteData) async {
    Directory dir = Platform.isAndroid
        ? await getExternalStorageDirectory()
        : await getApplicationDocumentsDirectory();
    File imageFile = new File(
        "${dir.path}/flutter/${DateTime.now().millisecondsSinceEpoch}.png");
    imageFile.createSync(recursive: true);
    imageFile.writeAsBytesSync(byteData.buffer.asUint8List(0));
    return imageFile.path;
  }
}

If you have any troubles, please, create an issue

所属分类

源码仓库

使用此插件的项目

暂无相关数据。