widgets_to_image
一個強大的 Flutter 套件,可將任何小部件轉換為高品質影像,支援自訂格式、品質設定與進階功能。
使用 Flutter 將您的組件導出為影像
{"sdk":"flutter"}{"sdk":"flutter"}^2.0.0以下為英文專案原文快照,最新內容請造訪 GitHub。
A powerful Flutter package to convert any widget into high-quality images with customizable formats, quality settings, and advanced features.
Experience the package capabilities with our modern, professional demo featuring:
Demo
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';
// 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();
// Capture with custom options
Uint8List? bytes = await controller.capture(
options: const CaptureOptions(
format: ImageFormat.jpeg,
pixelRatio: 3.0,
quality: 95,
waitForAnimations: true,
delayMs: 100,
),
);
// 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,
);
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'),
),
],
);
},
)
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');
}
controller.onCaptureStart = () {
print('Capture started');
};
controller.onCaptureComplete = (bytes) {
print('Capture completed with ${bytes.length} bytes');
};
controller.onCaptureError = (error) {
print('Capture failed: ${error.message}');
};
| 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.png - PNG format with transparency supportImageFormat.jpeg - JPEG format with quality controlImageFormat.rawRgba - Raw RGBA pixel dataImageFormat.rawUnmodified - Raw unmodified pixel datacapture({CaptureOptions options}) - Capture with custom optionscapturePng({...}) - Convenience method for PNG capturecaptureJpeg({...}) - Convenience method for JPEG capturedispose() - Clean up resourcescontroller - The capture controller (required)child - The widget to captureclipBehavior - Whether to clip the child widgetThe package includes a comprehensive example app demonstrating:
Run the example:
cd example
flutter run
| Platform | Support |
|---|---|
| Android | ✅ |
| iOS | ✅ |
| Web | ✅ |
| macOS | ✅ |
| Windows | ✅ |
| Linux | ✅ |
Contributions are welcome! Please read our contributing guidelines and submit pull requests to our repository.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by Mohamed Abdo phone
phone