How to implement a share button in Flutter app?
Asked Answered
L

6

36

In my app, I have a button and I need it to share Facebook when this button is clicked?

Ledaledah answered 28/5, 2018 at 22:9 Comment(2)
You want to share on Facebook? or Just want share ?Oleary
on facebook @dhuma1981Ledaledah
L
47

Check out flutter share plugin

https://pub.dev/packages/share_plus

Share.share('check out my website https://example.com');

Then users can choose to use facebook or any other service that supports share interface

Lues answered 28/5, 2018 at 23:54 Comment(3)
Do you have example of how to post to fb timeline?Gorgonzola
you need to have installed facebook on your phone, than facebook will be in the options to choose from once you click shareLues
What about a plugin that supports flutter web?Tosha
P
5

You can try this package: https://pub.dev/packages/wc_flutter_share

This package supports sharing image + text both.

You can use this like:

await WcFlutterShare.share(  
    sharePopupTitle: 'share',  
    subject: 'This is subject',  
    text: 'This is text',  
    fileName: 'share.png',  
    mimeType: 'image/png',  
    bytesOfFile: bytes.buffer.asUint8List());

Note: On facebook you can't share image and text at the same time. You can either share text or image.

Plenty answered 31/5, 2019 at 10:1 Comment(1)
This package is no longer supported.Raster
T
5

This is for us Flutter noobs, a more complete solution.

  1. Add this at the top of your .dart file:

    
    import 'package:share/share.dart';
    
    
  2. Run this command in your Terminal:

    
    flutter pub add share
    
    
  3. Add the share code into the button code in onPressed:

    
    onPressed: () {
                Share.share('check out my website https://example.com', subject: 'Look what I made!');
    }),
    
    

More information about the share package can be found on the link below:

https://pub.dartlang.org/packages/share

Teleran answered 25/4, 2021 at 19:39 Comment(1)
This package is no longer supported. This plugin has been replaced by the Flutter Community Plus Plugins version, share_plus pub.dev/packages/share_plusClegg
W
4

try this: https://github.com/d-silveira/flutter-share.git

if you want to share something other than text.

it allows sharing text/image/file with a simple named constructor instantiation Share.image(path: <PATH>, title: <TITLE>) and then call share() on it

just follow the instructions for further details on the README or see a working example in the example project folder.

It is fully functional for Android, the IOS part is currently being developed to match the Android part.

Wriggle answered 1/6, 2018 at 17:32 Comment(1)
Does it now support ios? Do you have example of how to post to fb timeline?Gorgonzola
A
1

share_plus 3.0.4: https://pub.dev/packages/share_plus A Flutter plugin to share content from your Flutter app via the platform's share dialog.

Run this command with Flutter:

$ flutter pub add share_plus

This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):

dependencies:
  share_plus: ^3.0.4
  • Import the library
    import 'package:share_plus/share_plus.dart';
  • Then invoke the static share method anywhere in your Dart code.
Share.share('hey! check out this new app https://play.google.com/store/search?q=pub%3ADivTag&c=apps');
  • The share method also takes an optional subject that will be used when sharing to email.
Share.share('hey! check out this new app https://play.google.com/store/search?q=pub%3ADivTag&c=apps', subject: 'DivTag Apps Link');

Platform Support : Android | iOS | MacOS | Web | Linux | Windows

Note

share 2.0.4 : https://pub.dev/packages/share This plugin has been replaced by the Flutter Community Plus Plugins version, to share_plus. No further updates are planned to this plugin, and they are encourage all users to migrate to the Plus version.

Aguste answered 15/10, 2021 at 2:52 Comment(3)
Does anyone know why this plugin does not share text on facebook and fb msger?!Nicolasanicolau
Don't know exactly, but the reason may be privacy policy, limitations, and security at different platforms.Aguste
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 (developers.facebook.com/docs/sharing) or search for other Flutter plugins implementing this SDK. More information can be found in this issue.Monarchal
G
0

You can try this package:

https://pub.dev/packages/share_plus

 Share.shareXFiles([XFile(file.path)], text: 'filename');
Groundwork answered 15/2 at 5:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.