Sharing an image

Flutter tips Published on

Flutter makes it easy to let your users share images directly from your app. This uses the native sharing options on their device, giving them a smooth experience!

How to Share an Image Easily

The simplest way to share an existing image file is by using the share_plus package.

Step 1: Add share_plus

First, add share_plus to your pubspec.yaml file.

Step 2: Share Your Image

Once installed, sharing an image file is straightforward. You'll need the path to your image file.

import 'package:share_plus/share_plus.dart';

await SharePlus.instance.share(ShareParams(
  files: [XFile(imageFile.path)],
  text: 'Check out this...',
  subject: 'My subject',
));

This code will open the native share sheet, allowing users to pick where they want to send the image.

Bonus: Share a Widget as an Image!

Imagine wanting to share a custom chart or a user's progress. You can actually turn any Flutter widget into an image and then share it!

Capturing a Widget Screenshot

To do this, you'll need the screenshot package. It helps you "capture" any widget on your screen.

Steps to Capture and Share

  1. Install Packages: Add both screenshot and path_provider (to save the image temporarily) to your pubspec.yaml.
  2. Create a ScreenshotController: This helps manage the capture.
  3. Capture the Widget: Use the controller to capture the widget.
  4. Save Temporarily: Save the captured image bytes to a temporary file.
  5. Share: Use share_plus with the path to your temporary image file.

Here's an example:

import 'package:share_plus/share_plus.dart';
import 'package:screenshot/screenshot.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import 'dart:typed_data'; // For Uint8List

Future<void> shareMyWidget(BuildContext context) async {
  final screenshotController = ScreenshotController();

  // This is the widget you want to capture and share.
  // Replace `Text('My Awesome Stats!')` with your actual custom widget.
  final shareableWidget = MaterialApp(
    debugShowCheckedModeBanner: false, // Hide debug banner
    home: Scaffold(
      backgroundColor: Colors.transparent,
      body: Center(
        child: Text('My Awesome Stats!', style: TextStyle(fontSize: 24, color: Colors.black)), 
      ),
    ),
  );

  try {
    final Uint8List? imageBytes = await screenshotController.captureFromWidget(
      shareableWidget,
      pixelRatio: 2.0, // For better quality
      context: context, // Needed to build the widget tree
    );

    if (imageBytes == null) {
      print('Image capture failed.');
      return; // Handle if capture fails
    }

    final tempDir = await getTemporaryDirectory();
    final fileName = 'my_stats.png';
    final imageFile = File('${tempDir.path}/$fileName');
    await imageFile.writeAsBytes(imageBytes);

    await SharePlus.instance.share(ShareParams(
      files: [XFile(imageFile.path)],
      text: 'Check out my goals progress! 🎉',
      subject: 'My KUBBO Stats',
    ));
  } catch (e) {
    print('Error sharing widget: $e');
  }
}

This powerful method lets you create and share dynamic content right from your app's UI. Make your app's sharing experience top-notch!

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

What is the easiest way to share an image in Flutter?

The easiest way is to use the `share_plus` package, providing the image file path.

Can I share a specific part of my app's screen?

Yes, you can capture a widget as an image using the `screenshot` package and then share that image file.

What packages do I need to share a widget as an image?

You need the `screenshot` package to capture the widget and `path_provider` to save it temporarily, along with `share_plus` to perform the actual sharing.

How do I save the captured widget image before sharing?

You save the `Uint8List` bytes returned by the `screenshot` package's `captureFromWidget` method to a temporary file using `path_provider` and `dart:io`.

Is `share_plus` compatible with both Android and iOS?

Yes, `share_plus` is designed to provide native sharing capabilities on both Android and iOS platforms.

Read more
You may also be interested in
Getting device timezone  blog card image
Getting device timezone
Published on 2025-11-28T16:54:52.958Z
Render widget outside of parent bounds  blog card image
Render widget outside of parent bounds
Published on 2025-11-28T16:57:11.793Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved