Open In App

Flutter – Share Plus Library

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 – 

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,




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.




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:




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:


Article Tags :