How to Add a Share Button in a Flutter App
In this article
Yes, you can add a share button to a Flutter app by using the share_plus package and a few lines of Dart code.
What You'll Need
- Flutter SDK (stable channel recommended)
- Android Studio, VS Code, or another IDE with Flutter support
- Internet connection to fetch packages from pub.dev
- Basic knowledge of Flutter widgets and state management
Step-by-Step
- Open your project. Launch your IDE and open the existing Flutter project where you want the share button to appear.
- Add the
share_plusdependency. Openpubspec.yamland underdependenciesadd:share_plus: ^8.0.0
Save the file and runflutter pub getin the terminal. The version number may be newer; use the latest stable version listed on pub.dev. - Import the package. In the Dart file where you will place the button, add:
import 'package:share_plus/share_plus.dart';
- Create a reusable widget. It is good practice to encapsulate the share logic in its own widget. Example:
class ShareButton extends StatelessWidget { final String text; final String? subject; const ShareButton({Key? key, required this.text, this.subject}) : super(key: key); @override Widget build(BuildContext context) { return IconButton( icon: const Icon(Icons.share), onPressed: () => Share.share(text, subject: subject), ); } }This widget usesIconButtonto display the standard share icon. Adjust the icon or style as needed. - Place the widget in your UI. Insert
ShareButtonwherever a share action makes sense – for example, in anAppBaractions list or at the bottom of a detail screen.AppBar( title: const Text('Article'), actions: [ ShareButton(text: articleUrl, subject: articleTitle), ], );ReplacearticleUrlandarticleTitlewith the actual strings you wish to share. - Handle platform‑specific permissions (optional). For Android, the package works out‑of‑the‑box because it uses an implicit intent. For iOS, ensure your
Info.plistcontains theNSPhotoLibraryAddUsageDescriptionkey only if you share images; otherwise no extra configuration is required. - Test on real devices. Run
flutter runon an Android phone and an iOS device. Tap the share icon; the native share sheet should appear with options like messaging, email, and social apps. - Optional: Share files or images. If you need to share a file, first write the file to the device's temporary directory, then call:
Share.shareFiles([filePath], text: 'Check this out');
Remember to clean up temporary files after sharing to avoid storage bloat. - Commit your changes. Add the updated
pubspec.yamland any new Dart files to version control.
Common Mistakes
- Forgetting to run
flutter pub getafter editingpubspec.yaml. The IDE may show a missing import error, but the package is not actually downloaded. - Using a hard‑coded URL that contains spaces or special characters without encoding. Encode the string with
Uri.encodeFullif necessary. - Attempting to share a file that does not exist. Verify the file path with
File.exists()before callingshareFiles. - Placing the share button inside a widget that rebuilds frequently (e.g., inside a
StreamBuilder) without making the button stateless. This can cause unnecessary rebuilds and UI jitter. - Neglecting iOS privacy keys when sharing images. The app will crash at runtime if the required key is missing.
Tips
- Wrap the share call in a
try / catchblock to gracefully handle unexpected platform errors. - Use
Share.shareWithResultif you need to know whether the user actually completed the share action. - Provide a fallback UI for platforms that do not support the native share sheet (e.g., web). The
share_pluspackage includes a web implementation that copies the text to the clipboard. - Keep the shared text concise; long messages may be truncated by some target apps.
- When sharing images, compress them to a reasonable size (e.g., under 1 MB) to avoid memory pressure on low‑end devices.
Troubleshooting
If the share sheet does not appear or the app crashes, follow these steps in order:
- Restart the app. A cold start clears any transient state that might be blocking the intent.
- Check package installation. Run
flutter pub deps | grep share_plusto confirm the package is listed. - Verify platform configuration. On Android, ensure
android/app/src/main/AndroidManifest.xmlcontains theandroid:exportedattribute for any activities you added manually. On iOS, openRunner.xcworkspaceand confirmInfo.plistincludes required privacy keys. - Update Flutter and plugins. Run
flutter upgradeandflutter pub upgradeto get the latest stable versions. - Inspect console logs. Use
flutter runand look for error messages related toShareor intent handling. - Test on another device. Some OEM Android skins restrict implicit intents; testing on a stock Android emulator can isolate the issue.
- Re‑install the app. Uninstall from the device, then run
flutter runagain. This clears cached intent filters. - Fallback to manual sharing. If the package consistently fails, you can fall back to copying the text to the clipboard using
Clipboard.setDataand showing aSnackBarprompting the user to paste manually.
Frequently Asked Questions
Can I share rich media like videos with share_plus?
Yes. Use Share.shareFiles and pass the video file path. Make sure the file is stored in a location accessible to other apps, such as the temporary directory.
Do I need to add any Android permissions?
For plain text sharing, no extra permission is required. When sharing files, the package automatically adds a FileProvider entry, so you do not need to request storage permission manually.
Will the share button work on Flutter web?
The package includes a web implementation that copies the share text to the clipboard and shows a small toast. It does not open a native share sheet because browsers do not expose one.
How can I customize the appearance of the share sheet?
The native share sheet is controlled by the operating system; Flutter cannot modify its layout. You can only influence the content you pass (text, subject, files).
Is there a way to know which app the user selected?
On Android, the intent does not return the selected target. On iOS, the API also does not expose that information. The package therefore cannot report the chosen app.
What if I need to share a deep link that opens my app?
Generate a URL that follows your app's custom scheme (e.g., myapp://article/123) and pass it as the share text. Ensure the scheme is registered in both Android AndroidManifest.xml and iOS Info.plist so that other apps can open it.