share_api 0.0.5

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new72

Flutter Share API Plugin

Flutter Plugin for sharing contents to social media.

At this moment, this plugin is Android-only. Adding iOS compatibility is the highest priority on the development road map.

Introduction

This plugin is still in development. Specifications and APIs may change in the subsequent versions. Use this at your own risk.

Available Features

PlatformAndroidiOSActivity Return ValueNotes
System UIText, Images, and Files
Facebook StoryImages (Background and Sticker)
Instagram StoryImages (Background and Sticker)

Configuration

Sharing Images and Files

In order to share files and/or images, you will need to add the following:

Android

In the android/app/src/main/AndroidManifest.xml, add the following in the manifest/application:

<!-- Add FileProvider in order to access the shared file -->
<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.com.albertalrisa.share_api"
        android:exported="false"
        android:grantUriPermissions="true">
    <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

After adding the File Provider to the manifest, create a file named file_paths.xml in the app/src/main/res/xml folder:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <cache-path name="images" path="/"/>
</paths>

Usage

The module is accessed statically by calling ShareApi.via[module].[function]().

The following functions are available:

ModuleFunction
System UIshareText()
shareFile()
shareImage()
FacebookshareToStory()
InstagramshareToStory()

Return Values

All share functions will return an integer representing the return value of the activity called. If the function does not support obtaining the operation result, ShareResult.undefined or 0x00 is given. Otherwise, the return value will be one of three possible values, which are ok, canceled, and failed. As of now, only Facebook responds with proper value, therefore running the other functions will always return undefined.

There are four possible values:

ValueMeaning
ShareResult.undefined (0x00)The share function does not support return values
ShareResult.ok (0x01)The share function has completed successfully, and the content has been shared
ShareResult.canceled (0x02)The user canceled the share action and return back to the application
ShareResult.failed (0x03)The share function failed to complete. Mostly due to exceptions and other errors

Development Road Map

  • iOS Compatibility for System UI, Facebook Story, and Instagram Story
  • Examples and Documentation Revamp

Contributing

Feel free to contribute by opening issues or submitting PRs through GitHub.

Change Log (share_api)

0.0.5

  • Nullable Problem Fix
  • Fix some variables being declared as nullable where a non-nullable one is expected

0.0.4

  • Provider Authorities
  • Change the provider authorities to match the project package name, reducing chance of collision (breaking change)
  • Fix an error caused by Kotlin implicitly convert the arguments from Flutter into a nullable type
  • Documentation is updated to match the new provider authorities setting

0.0.3

  • Instagram Share Result Fix and Default Function Removal
  • Instagram should now return ShareResult.unknown instead of ShareResult.canceled (breaking change)
  • Built in platformVersion() function from Flutter default plugin example has been removed (breaking change)
  • Documentation now includes the return value explanation

0.0.2

  • Documentation Revamp
  • Improve the documentation, especially on adding FileProvider in Android

0.0.1

  • Initial Release
  • Add System UI, Facebook Story, and Instagram Story API functions
  • Compatible only on Android

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:share_api/composers/facebook_story.dart';
import 'package:share_api/share_api.dart';

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

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

class _MyAppState extends State<MyApp> {
  Map<String, String> isInstalled = {
    "facebook": "unknown",
    "instagram": "unknown",
    "system": "unknown",
  };

  @override
  void initState() {
    super.initState();
    getInstalledPackages();
  }

  Future<void> getInstalledPackages() async {
    try {
      isInstalled["facebook"] =
          await ShareApi.viaFacebook.isPackageInstalled() ? "true" : "false";
    } on PlatformException {
      isInstalled["facebook"] = 'Failed to get facebook status.';
    }
    try {
      isInstalled["instagram"] =
          await ShareApi.viaInstagram.isPackageInstalled() ? "true" : "false";
    } on PlatformException {
      isInstalled["instagram"] = 'Failed to get instagram status.';
    }
    try {
      isInstalled["system"] =
          await ShareApi.viaSystemUI.isPackageInstalled() ? "true" : "false";
    } on PlatformException {
      isInstalled["system"] = 'Failed to get system status.';
    }

    setState(() {});

    if (!mounted) return;
  }

  bool _isLoading = false;

  void shareToInstagramStory() async {
    var image = await rootBundle.load('assets/image.jpg');
    var bytedata = image.buffer.asUint8List();
    var sticker = await rootBundle.load('assets/image.png');
    var stickerdata = sticker.buffer.asUint8List();
    var composer = FacebookStoryComposer(
      backgroundAsset: bytedata,
      backgroundMediaType: 'image/*',
      stickerAsset: stickerdata,
      stickerMediaType: 'image/*',
      topBackgroundColor: Color(0xFFFF0000),
      bottomBackgroundColor: Color(0xFF00FF00),
    );
    ShareApi.viaInstagram.shareToStory(composer).then((response) {
      print('Instagram $response');
    });
  }

  void shareToStory() async {
    var image = await rootBundle.load('assets/image.jpg');
    var bytedata = image.buffer.asUint8List();
    ShareApi.viaFacebook.setAppId("0000000000000000");
    var composer = FacebookStoryComposer(
      backgroundAsset: bytedata,
      backgroundMediaType: 'image/*',
//      stickerAsset: bytedata,
//      stickerMediaType: 'image/*',
//      topBackgroundColor: Color(0xFFFF0000),
//      bottomBackgroundColor: Color(0xFF00FF00),
    );
    ShareApi.viaFacebook.shareToStory(composer).then((response) {
      print('Facebook $response');
    });
  }

  void shareImage() async {
    setState(() {
      _isLoading = true;
    });
    var image = await rootBundle.load('assets/image.jpg');
    var bytes = image.buffer.asUint8List();
    ShareApi.viaSystemUI.shareImage(bytes, imageType: 'image/png');

    setState(() {
      _isLoading = false;
    });
  }

  void shareText() {
    ShareApi.viaSystemUI.shareText("Shared!");
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Center(
          child: Stack(
            fit: StackFit.expand,
            children: <Widget>[
              Image.asset('assets/image.jpg'),
              Container(
                alignment: Alignment(0.5, 1.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  mainAxisSize: MainAxisSize.min,
                  children: <Widget>[
                    Column(
                      children:
                          ["facebook", "instagram", "system"].map((handler) {
                        return Text(isInstalled[handler]);
                      }).toList(),
                    ),
                    RawMaterialButton(
                      onPressed: () {
                        shareToInstagramStory();
                      },
                      fillColor: Colors.lightBlue,
                      splashColor: Colors.lightBlueAccent,
                      child: _isLoading
                          ? Center(child: CircularProgressIndicator())
                          : Text(
                              'Share File To Instagram Story',
                              style: TextStyle(color: Colors.white),
                            ),
                    ),
                    RawMaterialButton(
                      onPressed: () {
                        shareToStory();
                      },
                      fillColor: Colors.lightBlue,
                      splashColor: Colors.lightBlueAccent,
                      child: _isLoading
                          ? Center(child: CircularProgressIndicator())
                          : Text(
                              'Share File To Facebook Story',
                              style: TextStyle(color: Colors.white),
                            ),
                    ),
                    RawMaterialButton(
                      onPressed: shareImage,
                      fillColor: Colors.lightBlue,
                      splashColor: Colors.lightBlueAccent,
                      child: _isLoading
                          ? Center(child: CircularProgressIndicator())
                          : Text(
                              'Share Image Through API',
                              style: TextStyle(color: Colors.white),
                            ),
                    ),
                    RawMaterialButton(
                      onPressed: shareText,
                      fillColor: Colors.lightBlue,
                      splashColor: Colors.lightBlueAccent,
                      child: Text(
                        'Share Text Through API',
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  share_api: ^0.0.5

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:share_api/share_api.dart';
  
Version Uploaded Documentation Archive
0.0.5 Oct 12, 2018 Go to the documentation of share_api 0.0.5 Download share_api 0.0.5 archive
0.0.4 Oct 12, 2018 Go to the documentation of share_api 0.0.4 Download share_api 0.0.4 archive
0.0.3 Oct 1, 2018 Go to the documentation of share_api 0.0.3 Download share_api 0.0.3 archive
0.0.2 Sep 28, 2018 Go to the documentation of share_api 0.0.2 Download share_api 0.0.2 archive
0.0.1 Sep 28, 2018 Go to the documentation of share_api 0.0.1 Download share_api 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
48
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Oct 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4
  • Flutter: 0.9.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
path ^1.6.2 1.6.2
path_provider ^0.4.1 0.4.1
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8