Skip to content
Related Articles

Related Articles

Flutter – Share Plus Library

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 31 Jan, 2022

In Flutter, share_plus is a library to share content across different platforms. In this article, we are going to create a simple app to show its functionality. To do so follow the steps – 

  • Add the dependency in the pubspec.yaml file
  • Import the dependency in the main.dart file
  • Use StatefulWidget to structure the application
  • Create a method to make use of library

Now, let us move ahead with the details.

Install the dependency:

Open the pubspec.yaml file of the project and add the share_plus library under the dependencies section of the file.

Import the library:

In main.dart, import the following library,

Dart




import 'package:share_plus/share_plus.dart';


Create a method

We need to create an async method _onShare(), that will be invoked when we click the share button to share the information. There are two different methods to share two types of different content:

  1. Sharing files using Share.shareFiles()
  2. Sharing content using Share.share()

Here, we are sharing content that includes text. The subject property is optional, as it will be only used when sending content over email. We have set the box position that will be rendered on the screen to ask for methods of sharing like WhatsApp, Email, Bluetooth, etc. So, that it can render itself properly according to a number of sharing apps available on the device.

Dart




void _onShare(BuildContext context) async {
   final box = context.findRenderObject() as RenderBox?;
    
  // subject is optional but it will be used 
  // only when sharing content over email
   await Share.share(text,
       subject: link,
       sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
 }


Full Source Code:

Dart




import 'package:flutter/material.dart';
import 'package:share_plus/share_plus.dart';
  
void main() {
  runApp(const MyApp());
}
  
class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);
  
  @override
  MyAppState createState() => MyAppState();
}
  
class MyAppState extends State<MyApp> {
  String text = '';
  String link = '';
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      color: Colors.green,
      debugShowCheckedModeBanner: false,
      title: 'Flutter Share Plus',
      theme: ThemeData(primarySwatch: Colors.green),
      home: Scaffold(
          appBar: AppBar(
            title: const Text('GeeksForGeeks'),
            backgroundColor: Colors.green,
            centerTitle: true,
          ),
          body: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(24.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  TextField(
                    decoration: const InputDecoration(
                      labelText: 'Text:',
                      hintText: 'Enter anything to share',
                    ),
                    maxLines: 2,
                    onChanged: (String value) => setState(() {
                      text = value;
                    }),
                  ),
                  TextField(
                    decoration: const InputDecoration(
                      labelText: 'Subject:',
                      hintText: 'Enter subject to share',
                    ),
                    maxLines: 2,
                    onChanged: (String value) => setState(() {
                      link = value;
                    }),
                  ),
                  const Padding(padding: EdgeInsets.only(top: 12.0)),
                  Builder(
                    builder: (BuildContext context) {
                      return ElevatedButton(
                        onPressed:
                            text.isEmpty ? null : () => _onShare(context),
                        child: const Text('Share'),
                      );
                    },
                  ),
                ],
              ),
            ),
          )),
    );
  }
  
  void _onShare(BuildContext context) async {
    final box = context.findRenderObject() as RenderBox?;
    await Share.share(text,
        subject: link,
        sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
  }
}


Output:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!