Widgets to Image Conversion

Capturing Your Widgets as Images in Flutter with widgets_to_image_plus: A Seamless Experience

Shirsh Shukla
5 min readDec 23, 2024

Hello Flutter Developers!

Are you looking to enhance your Flutter applications by capturing and manipulating widgets as images? If so, you’re in the right place! Today, I want to introduce you to a powerful Flutter library called widgets_to_image_plus. This library makes it simple to turn any Flutter widget into an image, offering various functionalities that can help you customize and enhance the visual appeal of your applications. Whether you’re aiming to allow users to share their creations or generate dynamic content, widgets_to_image_plus is here to simplify that process.

What is widgets_to_image_plus?

widgets_to_image_plus is a versatile Flutter package designed specifically for capturing Flutter widgets as images. With this library, developers can easily convert widgets into different image formats, including PNG and JPEG, and apply various image manipulations to create unique visuals. This functionality opens up a wide range of possibilities for improving the interactivity and aesthetics of your apps.

Key Features

The package comes with an array of features that make image capturing and manipulation straightforward:

  1. Capture Widgets as Images: Convert any Flutter widget into an image effortlessly. You can capture complex UIs, simple texts, or any widget in your app.
  2. Image Manipulation: Apply transformations like:
  • Grayscale Conversion: Change your image to shades of gray for a classic look.
  • Watermark Addition: Overlay a watermark on your image, perfect for branding purposes.
  • Background Color Customization: Change the background color to enhance the visibility of your images.
  • Thumbnail Generation: Create smaller, compressed versions of images for better performance in lists or previews.

3. Image Effects: Enhance your images with various effects including:

  • Rotation: Rotate the captured image to fit your layout needs.
  • Color Inversion: Invert the colors of the image for a striking effect.
  • Sepia Tone: Apply a warm sepia filter to give your images a vintage look.
  • Brightness, Contrast, and Saturation Adjustments: Fine-tune the visual quality of your images.
  • Vignette Effects: Add a vignette effect to draw attention to the center of your image.
  • Borders and Rounded Corners: Customize the appearance of your images with borders and rounded corners.
  • Horizontal and Vertical Flipping: Easily flip your images horizontally or vertically for creative layouts.

Getting Started with widgets_to_image_plus

Installation

To begin using widgets_to_image_plus, you need to add it to your Flutter project. Open your pubspec.yaml file and include the dependency like this:

dependencies:
widgets_to_image_plus: ^0.0.1

After adding this line, run flutter pub get in your terminal to install the package.

Importing the Library

Next, import the library into your Dart file where you want to use it:

import 'package:widgets_to_image_plus/widgets_to_image_plus.dart';

Basic Example of Capturing a Widget

Let’s dive into a simple example that demonstrates how to use widgets_to_image_plus to capture a widget and display it as an image.

Full Example Code

Explanation of the Code

  1. RepaintBoundary: This widget is crucial because it marks the boundary of the widget you want to capture. The key assigned to it allows the WidgetToImagePlus controller to reference this specific widget.
  2. Capture Button: When the “Capture Image” button is pressed, it triggers the _captureImage function, which captures the widget and stores the resulting image in _imageBytes.
  3. Display Captured Image: If _imageBytes is not null, the captured image is displayed using Image.memory().

Advanced Image Capture Techniques

The widgets_to_image_plus library provides a variety of methods for capturing images with different effects and manipulations. Here’s a quick overview of some available methods:

  • Capture as Grayscale: This method captures the widget and converts it into a grayscale image.
final bytes = await _controller.captureAsGrayscale();
  • Capture with Watermark: Add a watermark to the captured image.
final bytes = await _controller.captureWithWatermark(watermark: "Your Watermark");
  • Capture with Custom Background: Change the background color of the captured image.
final bytes = await _controller.captureWithBackground(backgroundColor: Colors.red);
  • Capture with Rotation: Rotate the captured image by a specified angle.
final bytes = await _controller.captureWithRotation(angle: 0.5);
  • Generate Thumbnail: Create a thumbnail version of the widget.
final bytes = await _controller.generateThumbnail();

Full Example with Image Manipulations

To showcase various image manipulation techniques, you can enhance the previous example with more buttons for different capture methods. Here’s how you can implement that:

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Capture Widget as Image"),
),
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RepaintBoundary(
key: _controller.containerKey,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
child: Center(
child: Text(
'Hello, Flutter!',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white, fontSize: 24),
),
),
),
),
SizedBox(height: 20),
Wrap(
spacing: 10,
runSpacing: 20,
alignment: WrapAlignment.center,
children: [
ElevatedButton(
onPressed: _captureImage,
child: Text("Capture Image"),
),
ElevatedButton(
onPressed: () async {
final bytes = await _controller.captureAsGrayscale();
_setImage(bytes);
},
child: Text("Capture as Grayscale"),
),
ElevatedButton(
onPressed: () async {
final bytes = await _controller.captureWithWatermark(watermark: "Watermark");
_setImage(bytes);
},
child: Text("Capture with Watermark"),
),
ElevatedButton(
onPressed: () async {
final bytes = await _controller.captureWithBackground(backgroundColor: Colors.red);
_setImage(bytes);
},
child: Text("Capture with Background"),
),
ElevatedButton(
onPressed: () async {
final bytes = await _controller.generateThumbnail();
_setImage(bytes);
},
child: Text("Generate Thumbnail"),
},
// Add more buttons for other capture methods
],
),
SizedBox(height: 20),
if (_imageBytes != null)
Image.memory(_imageBytes!), // Display captured image
],
),
),
),
);
}

Practical Use Cases

Here are some practical scenarios where widgets_to_image_plus can enhance your Flutter applications:

  1. Social Media Sharing: Enable users to share images of their custom posts, designs, or achievements from your app. Imagine a drawing app where users can capture and share their artwork instantly!
  2. Report Generation: Capture screenshots of data visualizations, charts, or any relevant UI elements for reports and export them as images.
  3. Dynamic Previews: Create dynamic previews of custom UI components, allowing users to see how their designs would look before finalizing them.
  4. User Feedback: Allow users to capture and send screenshots of their app experiences, helping you gather valuable feedback for improvements.

Conclusion

The widgets_to_image_plus package is an incredible tool for Flutter developers looking to capture and manipulate widgets as images. With its extensive features and ease of use, you can create visually appealing applications that offer a richer user experience.

Whether you’re aiming to capture user-generated content, create branded images, or generate dynamic previews, this package provides all the tools you need. So, give widgets_to_image_plus a try in your next Flutter project and take your app’s visuals to the next level!

Feel free to share your thoughts or any questions in the comments below. Happy coding!

If you got something wrong? Mention it in the comments. I would love to improve. your support means a lot to me! If you enjoy the content, I’d be grateful if you could consider subscribing to my YouTube channel as well.

I am Shirsh Shukla, a creative Developer, and a Technology lover. You can find me on LinkedIn or maybe follow me on Twitter or just walk over my portfolio for more details. And of course, you can follow me on GitHub as well.

Have a nice day!🙂

https://drive.google.com/file/d/1hdC-E7Kf97NM3YzWKvpm5olb89kNrIcs/view

--

--

Shirsh Shukla
Shirsh Shukla

Written by Shirsh Shukla

SDE at Reliance Jio | Mobile Application Developer | Speaker | Technical Writer | community member at Stack Overflow | Organizer @FlutterIndore

No responses yet