TechBuzzPro

How to Add a Share Button in a Flutter App

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open your project. Launch your IDE and open the existing Flutter project where you want the share button to appear.
  2. Add the share_plus dependency. Open pubspec.yaml and under dependencies add:
    share_plus: ^8.0.0
    Save the file and run flutter pub get in the terminal. The version number may be newer; use the latest stable version listed on pub.dev.
  3. Import the package. In the Dart file where you will place the button, add:
    import 'package:share_plus/share_plus.dart';
  4. 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 uses IconButton to display the standard share icon. Adjust the icon or style as needed.
  5. Place the widget in your UI. Insert ShareButton wherever a share action makes sense – for example, in an AppBar actions list or at the bottom of a detail screen.
    AppBar(
      title: const Text('Article'),
      actions: [
        ShareButton(text: articleUrl, subject: articleTitle),
      ],
    );
        
    Replace articleUrl and articleTitle with the actual strings you wish to share.
  6. 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.plist contains the NSPhotoLibraryAddUsageDescription key only if you share images; otherwise no extra configuration is required.
  7. Test on real devices. Run flutter run on 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.
  8. 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.
  9. Commit your changes. Add the updated pubspec.yaml and any new Dart files to version control.

Common Mistakes

  • Forgetting to run flutter pub get after editing pubspec.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.encodeFull if necessary.
  • Attempting to share a file that does not exist. Verify the file path with File.exists() before calling shareFiles.
  • 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 / catch block to gracefully handle unexpected platform errors.
  • Use Share.shareWithResult if 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_plus package 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:

  1. Restart the app. A cold start clears any transient state that might be blocking the intent.
  2. Check package installation. Run flutter pub deps | grep share_plus to confirm the package is listed.
  3. Verify platform configuration. On Android, ensure android/app/src/main/AndroidManifest.xml contains the android:exported attribute for any activities you added manually. On iOS, open Runner.xcworkspace and confirm Info.plist includes required privacy keys.
  4. Update Flutter and plugins. Run flutter upgrade and flutter pub upgrade to get the latest stable versions.
  5. Inspect console logs. Use flutter run and look for error messages related to Share or intent handling.
  6. Test on another device. Some OEM Android skins restrict implicit intents; testing on a stock Android emulator can isolate the issue.
  7. Re‑install the app. Uninstall from the device, then run flutter run again. This clears cached intent filters.
  8. Fallback to manual sharing. If the package consistently fails, you can fall back to copying the text to the clipboard using Clipboard.setData and showing a SnackBar prompting 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.