Create an image from widget

Flutter tips Published on

Want to save a part of your Flutter app screen as an image? Here's a simple way to do it in just a few steps.

Step 1: Wrap Your Widget

First, put the widget you want to turn into an image inside a RepaintBoundary widget.

Make sure to give this RepaintBoundary a GlobalKey.

final GlobalKey _repaintBoundaryKey = GlobalKey();

// ... inside your build method or wherever your widget is ...

RepaintBoundary(
  key: _repaintBoundaryKey,
  child: YourWidgetHere(), // <-- This is the widget you want to export
)

Why Use RepaintBoundary?

The GlobalKey linked to the RepaintBoundary helps you find the specific part of the screen you want to capture. It lets you get the render object for that widget, which is needed to create the image.

Step 2: Export and Save

Next, you need a function to actually create the image from the widget and save it.

You can use the image_gallery_saver package for easy saving to the phone's gallery.

Here's how you might do it:

import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'dart:ui'; // Import for Image type
import 'dart:typed_data'; // Import for Uint8List
import 'package:flutter/rendering.dart'; // Import for RenderRepaintBoundary

Future<void> exportImage() async {
  try {
    RenderRepaintBoundary boundary = _repaintBoundaryKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
    ui.Image image = await boundary.toImage(pixelRatio: 3.0); // You can adjust pixelRatio
    ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
    Uint8List? pngBytes = byteData?.buffer.asUint8List();

    if (pngBytes != null) {
      final result = await ImageGallerySaver.saveImage(
        pngBytes,
        quality: 100, // Image quality
        name: "my_widget_image_${DateTime.now().millisecondsSinceEpoch}.png", // File name
      );

      // Optional: Handle the result (e.g., show a success message)
      print(result); // Check result status
    }
  } catch (e) {
    // Handle any errors during the process
    print("Error exporting image: $e");
  }
}

Saving to the Gallery

The code above uses the image_gallery_saver package. After getting the image data as bytes, this package makes it simple to save that data directly into the user's photo album on their device.

This process lets you quickly capture the visual output of any widget in your app!

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

How do I capture a widget as an image?

You wrap the widget in a `RepaintBoundary`, give it a `GlobalKey`, and then use the key to access its render object and create an image.

What is a RepaintBoundary?

A `RepaintBoundary` is a widget that makes its child a separate layer. This can improve performance by limiting repaints and is essential for capturing a specific widget's render output as an image.

How can I save the image to the phone's photo gallery?

After getting the image data as bytes, you can use a third-party package like `image_gallery_saver` to save the image file to the user's device gallery.

What is the pixelRatio used for?

The `pixelRatio` in the `toImage` method determines the resolution of the captured image. A higher value results in a sharper image, especially on high-density displays.

Read more
You may also be interested in
Using AI to your app using Gemini  blog card image
Using AI to your app using Gemini
Published on 2025-05-12T09:02:56.170Z
Replace type code with Union type  blog card image
Replace type code with Union type
Published on 2025-05-12T09:03:17.217Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved