share_extend 1.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

Language: English | 中文简体

ShareExtend #

pub package

调用系统分享的Flutter组件,支持分享文本、图片、视频和文件

安装 #

dependencies:
  share_extend: "^1.1.3"

iOS #

添加下面的key到工程的info.plist文件,路径 <project root>/ios/Runner/Info.plist,用于将分享的图片保存到相册

<key>NSPhotoLibraryAddUsageDescription</key>
<string>这里填写为什么需要相册写入权限的描述语句</string>

Android #

如果涉及到要分享存储空间里面的文件,需要用到读写存储空间权限的,请在项目的android模块的下,添加读写权限,路径为 <project root>/android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

导入 #

import 'package:share_extend/share_extend.dart';

使用 #


//分享文本
ShareExtend.share("share text", "text","android share panel title","share subject");

//分享图片 (例子中使用了一个image_picker的插件来实现图片的选择)
File f =
    await ImagePicker.pickImage(source: ImageSource.gallery);
ShareExtend.share(f.path, "image");

//分享视频
File f = await ImagePicker.pickVideo(
        source: ImageSource.gallery);
ShareExtend.share(f.path, "video");

//分享文件
Directory dir = Platform.isAndroid
    ? await getExternalStorageDirectory()
    : await getApplicationDocumentsDirectory();
File testFile = new File("${dir.path}/flutter/test.txt");
if (!await testFile.exists()) {
  await testFile.create(recursive: true);
  testFile.writeAsStringSync("test for share documents file");
}
ShareExtend.share(testFile.path, "file");

//分享多图(借助了MultiImagePicker来多选获取图片图片,由于该库没有提供文件路径,因此demo里面先将图片保存为图片再调用分享)
_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);
  }
  ShareExtend.shareMultiple(imageList, "image",subject: "share muti image");
}

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;
}

1.1.3 #

  • add option subject for sharing multiple images

1.1.2 #

  • rebuild the provider code on android platform
  • update sample code

1.1.1 #

  • add option param "share panel title" on android
  • add option param "subject" on both android and ios
  • update sample code with latest image_picker plugin version that fixed video picker bugs

1.1.0 #

  • 添加多图,多文件分享功能
  • 合并image_picker在ios13中选取视频不可用的补丁

1.0.9 #

  • 移除了Android端的内置存储的读写权限,改成由APP端来按需求配置

1.0.7 #

  • Android端修改了FileProvider的authorities,继承FileProvider,防止FileProvider冲突

1.0.6 #

  • 修复了android端分享应用沙盒内文件可能出错的bug
  • 优化了android端分享时的权限请求逻辑

1.0.5 #

  • Fix bugs when sharing videos to some apps like WeChat.

1.0.4 #

  • Updated to Support Latest Android Dependencies

0.0.1 #

  • TODO: Describe initial release.

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:multi_image_picker/multi_image_picker.dart';

import 'package:share_extend/share_extend.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Container(
          child: Center(
            child: Column(
              children: <Widget>[
                RaisedButton(
                  onPressed: () {
                    ShareExtend.share("share text", "text",
                        sharePanelTitle: "share text title",
                        subject: "share text subject");
                  },
                  child: Text("share text"),
                ),
                RaisedButton(
                  onPressed: () async {
                    File f = await ImagePicker.pickImage(
                        source: ImageSource.gallery);
                    if (f != null) {
                      ShareExtend.share(f.path, "image",
                          sharePanelTitle: "share image title",
                          subject: "share image subject");
                    }
                  },
                  child: Text("share image"),
                ),
                RaisedButton(
                  onPressed: () async {
                    File f = await ImagePicker.pickVideo(
                        source: ImageSource.gallery);
                    if (f != null) {
                      ShareExtend.share(f.path, "video");
                    }
                  },
                  child: Text("share video"),
                ),
                RaisedButton(
                  onPressed: () {
                    _shareStorageFile();
                  },
                  child: Text("share file"),
                ),
                RaisedButton(
                  onPressed: () {
                    _shareMultipleImages();
                  },
                  child: Text("share multiple images"),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  ///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);
    }
    ShareExtend.shareMultiple(imageList, "image", subject: "share muti image");
  }

  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;
  }

  ///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");
    }
    ShareExtend.share(testFile.path, "file");
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  share_extend: ^1.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:share_extend/share_extend.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Apr 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
meta ^1.0.5 1.1.8
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8