Share plus package in flutter : How to implement it?

URL: https://pub.dev/packages/share_plus
Supported Platforms: Android, iOS, Linux, macOS, Web, Windows
Popularity: 2890 Likes 

If you’re an avid smartphone user, odds are you’ve seen something on a mobile application that you wanted to share with a friend, for example, while browsing through your favorite social media platform or exploring items for sale.

Sharing files, text, and images between apps

In this tutorial, we’ll cover how to share texts and images from our sample application to other mobile applications.

Import the share_plus Package

In your Flutter Dart code, import the share_plus package:

import 'package:share_plus/share_plus.dart';

Here are some examples you can use in your project :

Share class: Provides methods for sharing content with other apps.
Share.share() method: Shares the provided content to other apps.
Share.shareFiles() method: Shares the provided files to other apps.
Share.shareImage() method: Shares the provided image to other apps.
Share.shareVideo() method: Shares the provided video to other apps.
Share.shareAudio() method: Shares the provided audio to other apps.
Share.shareContent() method: Shares the provided content to other apps wif custom sharing options.

How to share plain text :

Share.share('Hello Welcome to Flutterfever.com');
//subject is optional, and it is required for Email App.

How to Share Text with URL:

Share.share('Visit Flutterfever at https://www.flutterfever.com');

How to Share Images or Files in the Flutter App:

Share.shareFiles(['${directory.path}/image.jpg'], text: 'Great picture');
Share.shareFiles(['${directory.path}/image1.jpg', '${directory.path}/image2.jpg']);

Code:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Share Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: () {
              shareText('Hello, this is a shared message!');
            },
            child: Text('Share Message'),
          ),
        ),
      ),
    );
  }

  void shareText(String text) {
    Share.share(text);
  }
}

You can also share other types of content like URLs, files, etc., using different methods provided by the share package

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More