搜索插件、代码仓与 APP 分类

插件详情v2.0.1

widgets_to_image

一个强大的 Flutter 包,可将任何小部件转换为高质量图像,支持自定义格式、质量设置和高级功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2.6万
喜欢数
225
Pub 分数
160

安装插件

flutter pub add widgets_to_image

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

widgets_to_image 是一个 Flutter 包,可让开发者将任何 widget 转换为高质量图像,支持多种格式、可自定义的质量设置以及高级渲染选项。它非常适合在应用内直接生成截图、预览或可导出的资源。该包可在 Android、iOS、Web、macOS、Windows 和 Linux 等所有主要平台上运行,使其在跨平台项目中具有高度灵活性。通过简单的 API,它可以无缝集成到测试工作流、UI 预览和动态内容生成中。

适合场景

  • 程序化生成应用截图
  • 在开发中创建 UI 预览
  • 导出小部件为图像以共享
  • 自动化测试与视觉验证
  • 在Flutter应用中构建建筑设计工具

主要特点

  • 支持多种图像格式
  • 可自定义的图像质量和大小
  • 跨平台兼容性
  • 与现有控件的轻松集成
  • 高级渲染选项,如背景颜色和内边距

适合对象

  • 开发用户界面预览工具的开发者
  • 需要自动化截图生成的团队
  • 需要动态图像导出的应用
  • 测试自动化工程师
  • 注重设计的 Flutter 应用程序

使用建议

  • 渲染复杂控件时的性能影响
  • 大尺寸或高分辨率图像的内存使用情况
  • 需要正确处理异步渲染
  • 仅限静态图像输出(无动画)
  • 可能需要特定平台的配置才能获得最佳效果

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Widgets To Image

pub package License: MIT Demo

A powerful Flutter package to convert any widget into high-quality images with customizable formats, quality settings, and advanced features.

🌐 Live Demo

Try the interactive demo →

Experience the package capabilities with our modern, professional demo featuring:

  • 🎯 Basic widget capture examples
  • ⚙️ Advanced configuration options
  • 🎭 Animation capture demonstrations
  • 📱 Responsive design with Material 3

Demo

✨ Features

  • 🎯 Simple API - Convert any widget to image with just a few lines of code
  • 🖼️ Multiple Formats - Support for PNG, JPEG, Raw RGBA, and Raw Unmodified formats
  • ⚙️ Customizable Options - Control pixel ratio, quality, animations, and delays
  • 🎨 High Quality - Generate high-resolution images with custom pixel ratios
  • 🔄 Animation Support - Capture animated widgets with proper timing
  • 📱 Cross Platform - Works on iOS, Android, Web, macOS, Windows, and Linux
  • 🧪 Well Tested - Comprehensive test suite ensuring reliability
  • 📚 Rich Documentation - Detailed examples and API documentation

🚀 Getting Started

Installation

Add this to your package's pubspec.yaml file:

dependencies:
  widgets_to_image: ^2.0.0

Install it using the command line:

flutter pub get

Import the package:

import 'package:widgets_to_image/widgets_to_image.dart';

📖 Usage

Basic Usage
// Create a controller
WidgetsToImageController controller = WidgetsToImageController();

// Wrap your widget
WidgetsToImage(
  controller: controller,
  child: Container(
    width: 200,
    height: 100,
    color: Colors.blue,
    child: const Center(
      child: Text(
        'Hello World!',
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
    ),
  ),
)

// Capture the widget
Uint8List? bytes = await controller.capture();
Advanced Usage with Options
// Capture with custom options
Uint8List? bytes = await controller.capture(
  options: const CaptureOptions(
    format: ImageFormat.jpeg,
    pixelRatio: 3.0,
    quality: 95,
    waitForAnimations: true,
    delayMs: 100,
  ),
);
Convenience Methods
// Capture as PNG with high resolution
Uint8List? pngBytes = await controller.capturePng(
  pixelRatio: 3.0,
  waitForAnimations: true,
);

// Capture as JPEG with custom quality
Uint8List? jpegBytes = await controller.captureJpeg(
  quality: 90,
  pixelRatio: 2.0,
);
Reactive UI with Builder
WidgetsToImageBuilder(
  controller: controller,
  builder: (context, isCapturing, capture) {
    return Column(
      children: [
        if (isCapturing)
          const CircularProgressIndicator()
        else
          ElevatedButton(
            onPressed: () async {
              final bytes = await capture();
              // Handle the captured bytes
            },
            child: const Text('Capture Widget'),
          ),
      ],
    );
  },
)
Error Handling
try {
  final bytes = await controller.capture();
  // Success - use the bytes
} on CaptureException catch (e) {
  // Handle specific capture errors
  print('Capture failed: ${e.message}');
  if (e.originalError != null) {
    print('Caused by: ${e.originalError}');
  }
} catch (e) {
  // Handle other errors
  print('Unexpected error: $e');
}
Lifecycle Callbacks
controller.onCaptureStart = () {
  print('Capture started');
};

controller.onCaptureComplete = (bytes) {
  print('Capture completed with ${bytes.length} bytes');
};

controller.onCaptureError = (error) {
  print('Capture failed: ${error.message}');
};

🔧 API Reference

CaptureOptions
Property Type Default Description
pixelRatio double 1.0 The pixel ratio for the captured image
format ImageFormat ImageFormat.png The output image format
quality int 95 Quality for JPEG format (0-100)
waitForAnimations bool false Wait for animations before capturing
delayMs int 0 Additional delay in milliseconds
ImageFormat
  • ImageFormat.png - PNG format with transparency support
  • ImageFormat.jpeg - JPEG format with quality control
  • ImageFormat.rawRgba - Raw RGBA pixel data
  • ImageFormat.rawUnmodified - Raw unmodified pixel data
WidgetsToImageController Methods
  • capture({CaptureOptions options}) - Capture with custom options
  • capturePng({...}) - Convenience method for PNG capture
  • captureJpeg({...}) - Convenience method for JPEG capture
  • dispose() - Clean up resources
WidgetsToImage Properties
  • controller - The capture controller (required)
  • child - The widget to capture
  • clipBehavior - Whether to clip the child widget

📱 Example App

The package includes a comprehensive example app demonstrating:

  • Basic widget capture
  • Advanced options configuration
  • Animated widget capture
  • Different image formats
  • Error handling
  • Reactive UI updates

Run the example:

cd example
flutter run

🛠️ Platform Support

Platform Support
Android ✅
iOS ✅
Web ✅
macOS ✅
Windows ✅
Linux ✅

🤝 Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Flutter team for the amazing framework
  • Community contributors for feedback and improvements

📞 Support


Made with ❤️ by Mohamed Abdo phone

MacOS

phone

所属分类

源码仓库

使用此插件的项目