Introduction

The share plugin allows you to provide users with the ability to share content using native platform share dialogs.

In this article, you will build an example Flutter app that contains a list of alligators and add the ability to share the alligators with other users.

Prerequisites

share illustration for: Prerequisites

To complete this tutorial, you will need:

  • It is recommended to install plugins for your code editor:
  • Flutter and Dart plugins installed for Android Studio.
  • Flutter extension installed for Visual Studio Code.

This tutorial was verified with Flutter v2.0.6, Android SDK v31.0.2, and Android Studio v4.1.

Step 1 — Setting Up the Project

Once you have your environment set up for Flutter, you can run the following to create a new application:

				
					
flutter create <^>flutter_share_example<^>

				
			

Navigate to the new project directory:

				
					
cd <^>flutter create <^>flutter_share_example<^>

				
			

Using flutter create will produce a demo application that will display the number of times a button is clicked.

Step 2 — Adding the share Plugin

Open pubspec.yaml in your code editor and add the following plugin:

				
					
[label pubspec.yaml]

dependencies:

 flutter:

 sdk: flutter



 <^>share: 2.0.1<^>

				
			

Then, save the changes to your file.

Step 3 — Scaffolding the Project

We can then go ahead and update our main.dart file to contain a HomePage found at home_page.dart:

				
					
[label lib/main.dart]

import 'package:flutter/material.dart';

<^>import 'home_page.dart';<^>



void main() {

 runApp(MyApp());

}



class MyApp extends StatelessWidget {

 @override

 Widget build(BuildContext context) {

 return MaterialApp(

 title: 'Flutter Demo',

 theme: ThemeData(

 primarySwatch: Colors.blue,

 ),

 home: <^>MyHomePage()<^>,

 );

 }

}

				
			

Create a new home_page.dart file and open it with a code editor. Add the following lines of code:

				
					
[label lib/home_page.dart]

import 'package:flutter/material.dart';



class MyHomePage extends StatefulWidget {

 MyHomePage({Key key}) : super(key: key);



 @override

 _MyHomePageState createState() => _MyHomePageState();

}



class _MyHomePageState extends State<MyHomePage> {

 @override

 Widget build(BuildContext context) {

 return Scaffold(

 appBar: AppBar(

 title: Text('My favourite Alligators'),

 ),

 body: Container(),

 );

 }

}

				
			

MyHomePage will be a StatefulWidget that contains an AppBar and an empty Container.

Then, save the changes to your files.

Step 4 — Creating an Example Class

Next, you will need an Alligator class that contains a name and description, we'll use this to generate our list of alligators.

Create a new alligator_model.dart file and open it in your code editor. Add the following lines of code:

				
					
[label lib/alligator_model.dart]

import 'package:flutter/foundation.dart';



class Alligator {

 String name;

 String description;



 Alligator({@required this.name, @required this.description});

}

				
			

We can then create a List<Alligator> inside of MyHomePage and display them on screen:

				
					
[label lib/home_page.dart]

import 'package:flutter/material.dart';

&lt;^&gt;import 'alligator_model.dart';&lt;^&gt;



class MyHomePage extends StatefulWidget {

 MyHomePage({Key key}) : super(key: key);



 @override

 _MyHomePageState createState() =&gt; _MyHomePageState();

}



class _MyHomePageState extends State&lt;MyHomePage&gt; {

 &lt;^&gt;List&lt;Alligator&gt; alligators = [&lt;^&gt;

 &lt;^&gt;Alligator(&lt;^&gt;

 &lt;^&gt;name: 'Crunchy',&lt;^&gt;

 &lt;^&gt;description: 'A fierce Alligator with many teeth.',&lt;^&gt;

 &lt;^&gt;),&lt;^&gt;

 &lt;^&gt;Alligator(&lt;^&gt;

 &lt;^&gt;name: 'Munchy',&lt;^&gt;

 &lt;^&gt;description: 'Has a big belly, eats a lot.',&lt;^&gt;

 &lt;^&gt;),&lt;^&gt;

 &lt;^&gt;Alligator(&lt;^&gt;

 &lt;^&gt;name: 'Grunchy',&lt;^&gt;

 &lt;^&gt;description: 'Scaly Alligator that looks menacing.',&lt;^&gt;

 &lt;^&gt;),&lt;^&gt;

 &lt;^&gt;];&lt;^&gt;



 @override

 Widget build(BuildContext context) {

 return Scaffold(

 appBar: AppBar(

 title: Text('My favourite Alligators'),

 ),

 body: &lt;^&gt;Column(&lt;^&gt;

 &lt;^&gt;children: alligators&lt;^&gt;

 &lt;^&gt;.map((Alligator alligator) =&gt; Card(&lt;^&gt;

 &lt;^&gt;child: Column(&lt;^&gt;

 &lt;^&gt;children: &lt;Widget&gt;[&lt;^&gt;

 &lt;^&gt;ListTile(&lt;^&gt;

 &lt;^&gt;title: Text(alligator.name),&lt;^&gt;

 &lt;^&gt;subtitle: Text(alligator.description),&lt;^&gt;

 &lt;^&gt;),&lt;^&gt;

 &lt;^&gt;],&lt;^&gt;

 &lt;^&gt;),&lt;^&gt;

 &lt;^&gt;))&lt;^&gt;

 &lt;^&gt;.toList()&lt;^&gt;

 &lt;^&gt;),&lt;^&gt;

 );

 }

}

				
			

Add the List and map the contents of the List.

Step 5 — Adding the Share Functionality

In order to make it so that whenever the user clicks on a ListTile, we'll hook into the onTap functionality, calling a share function:

				
					
[label lib/home_page.dart]

import 'package:flutter/material.dart';

&lt;^&gt;import 'package:share/share.dart';&lt;^&gt;

import 'alligator_model.dart';



class MyHomePage extends StatefulWidget {

 MyHomePage({Key key}) : super(key: key);



 @override

 _MyHomePageState createState() =&gt; _MyHomePageState();

}



class _MyHomePageState extends State&lt;MyHomePage&gt; {

 List&lt;Alligator&gt; alligators = [

 Alligator(

 name: 'Crunchy',

 description: 'A fierce Alligator with many teeth.',

 ),

 Alligator(

 name: 'Munchy',

 description: 'Has a big belly, eats a lot.',

 ),

 Alligator(

 name: 'Grunchy',

 description: 'Scaly Alligator that looks menacing.',

 ),

 ];



 &lt;^&gt;share(BuildContext context, Alligator alligator) {&lt;^&gt;

 &lt;^&gt;final RenderBox box = context.findRenderObject();&lt;^&gt;



 &lt;^&gt;Share.share("${alligator.name} - ${alligator.description}",&lt;^&gt;

 &lt;^&gt;subject: alligator.description,&lt;^&gt;

 &lt;^&gt;sharePositionOrigin: box.localToGlobal(Offset.zero) &amp; box.size);&lt;^&gt;

 &lt;^&gt;}&lt;^&gt;



 @override

 Widget build(BuildContext context) {

 return Scaffold(

 appBar: AppBar(

 title: Text('My favourite Alligators'),

 ),

 body: Column(

 children: alligators

 .map((Alligator alligator) =&gt; Card(

 child: Column(

 children: &lt;Widget&gt;[

 ListTile(

 title: Text(alligator.name),

 subtitle: Text(alligator.description),

 &lt;^&gt;onTap: () =&gt; share(context, alligator),&lt;^&gt;

 ),

 ],

 ),

 ))

 .toList()

 ),

 );

 }

}

				
			

This code will import share, define a share method, and have it called with onTap.

Note: We need to ensure that we pass in the sharePositionOrigin for iPad devices by capturing the context.findRenderObject().

Compile your code and have it run in an emulator:

The main part of this is the Share function where we can supply a text and optional subject, which we're using to pass this to our messaging app in this example.

Conclusion

In this article, you built an example Flutter app that contains a list of alligators and added the ability to share the alligators with other users.

If you'd like to learn more about Flutter, check out our Flutter topic page for exercises and programming projects.