Original: d-silveira/flutter-share
A Flutter plugin to share content from your Flutter app via the platform's share dialog and receive shares from other apps on the platform (currently only on Android).
- add
share
share:
git:
url: https://github.com/seiyial/flutter-share.git
as a dependency in your pubspec.yaml file.
- Put this in
main.dart
or whichever file you are performing the outbound share:-
import 'package:share/share.dart';
- Create an instance of a
Share
using one of the following.
Share.plainText(text: <String>, title: <String>);
Share.file(path: <String>, mimeType: ShareType, title: , text: );
Share.image(path: , mimeType: , title: , text: );
Share.multiple(shares: List<Share>, mimeType: , title: );
Note: for each of the above methods, only the first argument is required.
- Call
.share({ Rect sharePositionOrigin })
on the instance. Example:-
Share _fileToShare = Share.plainText(text: '[email protected]');
final RenderBox _box = context.findRenderObject();
_fileToShare.share(sharePositionOrigin: _box.localToGlobal(Offset.zero) & _box.size);
// Another example
final RenderBox _box2 = context.findRenderObject();
Share.image(
path: "content://0@media/external/images/media/2129",
mimeType: ShareType.TYPE_IMAGE
).share(
sharePositionOrigin: _box2.localToGlobal(Offset.zero) & _box2.size
);
- In your Android
MainActivity
, replaceextends FlutterActivity
withextends FlutterShareReceiverActivity
.
MainActivity.java
// ...
import io.flutter.plugins.share.FlutterShareReceiverActivity;
// ...
// ...
// Originally FlutterActivity
public class MainActivity extends FlutterShareReceiverActivity {
// ...
}
// ...
- Your
main.dart
should receive a share.
import 'package:share/receive_share_state.dart';
- In your StatefulWidget replace your
extends State<T>
withextends ReceiveShareState<T>
and implement your mandatory@override void receiveShare(Share shareObj) { }
where you'll receive your shares.
finally call enableShareReceiving();
in your initState().
That's it!
Check out the example in the example project folder for a working example.
Currently only the Android part is complete (IOS part does the same as google's original version), but be on the lookout for new versions, as the IOS part is being worked on and will soon do all the same bells and whistles.
- [] Add instruction to put
android.intent.action.VIEW
(for viewing received files) andandroid.intent.action.SEND
in the AndroidManifest