GithubHelp home page GithubHelp logo

akvelon / flutter-share-plugin Goto Github PK

View Code? Open in Web Editor NEW
33.0 7.0 1.0 225 KB

A Flutter plugin to share content from your Flutter app via the platform's share dialog

License: MIT License

Kotlin 37.91% Ruby 12.36% Swift 23.02% Objective-C 2.43% Dart 24.28%

flutter-share-plugin's Introduction

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

flutter-share-plugin's People

Contributors

rustemmuzafarov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

amitpatil215

flutter-share-plugin's Issues

Null safety

Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

 - package:akvelon_flutter_share_plugin

Can you migrate the plugin to null safety?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.