s_screenshot 2.0.0
s_screenshot: ^2.0.0 copied to clipboard
A powerful Flutter package for capturing high-quality screenshots of widgets with multiple output formats (base64, bytes, file) and configurable options.
s_screenshot #
A powerful and flexible Flutter package for capturing high-quality screenshots of widgets with multiple output formats, cross-platform file saving, and comprehensive configuration options.
Features #
β¨ Multiple Output Formats
- Base64 encoded string
- Raw bytes (Uint8List)
- [BREAKING] Download to device with
file_saver(unified approach for all platforms)- Removed direct file save (use downloads instead)
π Cross-Platform File Saving
- Web: Browser downloads via
file_saver - Mobile (iOS/Android): Application documents directory
- Desktop (Windows/macOS/Linux): Downloads folder
- Unified API with callback-based platform handling
π¨ Configurable Options
- Custom pixel ratio for quality control
- PNG or raw RGBA format support
- Optional capture delay for animations
- Debug logging
- [NEW] Customizable file names for downloads
π‘οΈ Robust Error Handling
- Type-safe exceptions with detailed error messages
- Widget validation before capture
- Clear error reporting
- Platform-specific error guidance
π§ Easy to Use
- Simple API with sensible defaults
- Breaking changes from v1.0.0 - see migration guide
- Comprehensive example app
- Well-documented with platform setup guides
Demo #

Installation #
Add this to your package's pubspec.yaml file:
dependencies:
s_screenshot: ^2.0.0
Then run:
flutter pub get
Platform-Specific Setup #
iOS & macOS #
To enable file saving on iOS and macOS, please follow the configuration guide:
Quick summary:
- iOS: Add file sharing support to
Info.plist - macOS: Add downloads folder access to entitlements
Usage #
1. Basic Usage - Capture as Base64 #
import 'package:flutter/material.dart';
import 'package:s_screenshot/s_screenshot.dart';
class MyWidget extends StatelessWidget {
final GlobalKey _screenshotKey = GlobalKey();
Future<void> captureScreenshot() async {
try {
final base64String = await SScreenshot.capture(
_screenshotKey,
config: const ScreenshotConfig(
resultType: ScreenshotResultType.base64,
),
);
print('Screenshot captured: ${base64String.length} characters');
} on ScreenshotException catch (e) {
print('Error: ${e.message}');
}
}
@override
Widget build(BuildContext context) {
return RepaintBoundary(
key: _screenshotKey,
child: Container(
// Your widget content here
),
);
}
}
2. Capture as Bytes #
final bytes = await SScreenshot.capture(
_screenshotKey,
config: const ScreenshotConfig(
resultType: ScreenshotResultType.bytes,
pixelRatio: 2.0,
),
) as Uint8List;
// Use the bytes directly
await someApi.uploadImage(bytes);
3. Download Screenshot (Cross-Platform - Recommended) #
The easiest way to save files across all platforms (web, mobile, desktop):
import 'package:s_screenshot/s_screenshot.dart';
import 'package:file_saver/file_saver.dart';
Future<void> downloadScreenshot() async {
try {
// Captures and downloads in one step
await SScreenshot.captureAndDownload(
_screenshotKey,
fileName: 'my_screenshot.png',
pixelRatio: 3.0,
fileSaverCallback: (bytes, name) async {
await FileSaver.instance.saveFile(
name: name,
bytes: Uint8List.fromList(bytes),
mimeType: MimeType.png,
);
},
);
} on ScreenshotException catch (e) {
print('Error: ${e.message}');
}
}
5. Capture with Delay #
Useful for waiting for animations to complete:
final screenshot = await SScreenshot.capture(
_screenshotKey,
config: const ScreenshotConfig(
captureDelay: Duration(milliseconds: 500),
resultType: ScreenshotResultType.base64,
),
);
6. Custom Configuration #
final screenshot = await SScreenshot.capture(
_screenshotKey,
config: const ScreenshotConfig(
pixelRatio: 4.0, // Higher quality
format: ScreenshotFormat.png, // PNG or rawRgba
resultType: ScreenshotResultType.bytes,
shouldShowDebugLogs: true, // Enable logging
captureDelay: Duration(seconds: 1),
),
);
Configuration Options #
ScreenshotConfig #
| Parameter | Type | Default | Description |
|---|---|---|---|
pixelRatio |
double |
3.0 |
Pixel ratio for screenshot quality (higher = better quality but larger size) |
format |
ScreenshotFormat |
png |
Image format: png or rawRgba |
resultType |
ScreenshotResultType |
base64 |
Output format: base64, bytes, or download |
fileName |
String? |
null |
File name (required when resultType is download) |
shouldShowDebugLogs |
bool |
false |
Enable debug logging |
captureDelay |
Duration? |
null |
Delay before capturing (useful for animations) |
Result Types #
ScreenshotResultType.base64: Returns aStringwith base64-encoded imageScreenshotResultType.bytes: ReturnsUint8Listwith raw image bytesScreenshotResultType.download: Triggers cross-platform download (use withcaptureAndDownload()method)
API Methods #
SScreenshot.capture() #
Captures a widget screenshot with the specified configuration.
static Future<dynamic> capture(
GlobalKey key, {
ScreenshotConfig config = const ScreenshotConfig(),
}) async
SScreenshot.downloadScreenshot() #
Downloads screenshot bytes cross-platform using file_saver (web) or file system (native).
static Future<dynamic> downloadScreenshot(
List<int> bytes, {
required String fileName,
Future<void> Function(List<int>, String)? fileSaverCallback,
Future<File> Function(List<int>, String)? pathProviderCallback,
}) async
SScreenshot.captureAndDownload() #
Convenience method that combines capture and download in one step.
static Future<dynamic> captureAndDownload(
GlobalKey key, {
String? fileName,
double pixelRatio = 3.0,
Duration? captureDelay,
bool shouldShowDebugLogs = false,
Future<void> Function(List<int>, String)? fileSaverCallback,
Future<File> Function(List<int>, String)? pathProviderCallback,
}) async
Error Handling #
The package throws ScreenshotException with detailed error messages:
try {
final screenshot = await SScreenshot.capture(_key);
} on ScreenshotException catch (e) {
print('Screenshot failed: ${e.message}');
if (e.originalError != null) {
print('Original error: ${e.originalError}');
}
}
Common errors:
- Widget not yet rendered
- Widget not wrapped in
RepaintBoundary - Missing file path when using
fileresult type - Missing file name when using
downloadresult type - Failed to convert image to byte data
Important Notes #
- RepaintBoundary Required: Always wrap the widget you want to capture in a
RepaintBoundary:
RepaintBoundary(
key: _screenshotKey,
child: YourWidget(),
)
- Widget Must Be Rendered: Ensure the widget is visible and rendered before capturing:
await tester.pumpAndSettle(); // In tests
await Future.delayed(Duration(milliseconds: 100)); // In production if needed
-
Pixel Ratio Impact: Higher pixel ratios produce better quality but larger file sizes:
1.0: Low quality, small size2.0: Medium quality3.0: High quality (default)4.0+: Very high quality, large size
-
Cross-Platform Downloads: When using
captureAndDownload()withfileSaverCallback:- Make sure to import
file_saverpackage - Check iOS & macOS Setup Guide for platform requirements
- Make sure to import
Dependencies #
flutter: ^3.0.0universal_io: ^2.2.2 (for cross-platform File handling)file_saver: ^0.3.1 (for cross-platform downloads)path_provider: ^2.1.0 (optional, for native file paths)
These dependencies are automatically available through the package exports.
Example #
Check out the example directory for a complete working app demonstrating all features.
To run the example:
cd example
flutter pub get
flutter run
Testing #
The package includes comprehensive tests covering:
- Base64 output
- Bytes output
- File output
- Custom pixel ratios
- Error handling and validation
- Configuration defaults
Run tests with:
flutter test
Changelog #
See CHANGELOG.md for version history and detailed changes.
What's New in v2.0.0 #
π Major Breaking Release - Unified Cross-Platform Approach
Breaking Changes β οΈ #
- REMOVED:
ScreenshotResultType.file- usecaptureAndDownload()instead - REMOVED:
ScreenshotConfig.filePathparameter - Rationale: Unified file handling across all platforms (web, mobile, desktop) via downloads
New Features β¨ #
- β¨ New
downloadScreenshot()method for cross-platform file saving - β¨ New
captureAndDownload()convenience method - β¨ Support for
file_saverpackage (works on all platforms) - β¨ Callback-based architecture for flexible platform handling
- β¨ Added
universal_iofor unified File API - π Updated
ScreenshotResultTypeenum with unifieddownloadoption - π Enhanced
ScreenshotConfigwithfileNameparameter only (no more filePath) - π Comprehensive iOS & macOS setup guide
Migration from v1.0.0 β v2.0.0 #
Old approach (v1.0.0):
// Save directly to file
final file = await SScreenshot.capture(
key,
config: ScreenshotConfig(
resultType: ScreenshotResultType.file,
filePath: '/path/to/file.png',
),
) as File;
New approach (v2.0.0):
// Use captureAndDownload with callback
await SScreenshot.captureAndDownload(
key,
fileName: 'screenshot.png',
fileSaverCallback: (bytes, name) async {
await FileSaver.instance.saveFile(
name: name,
bytes: bytes,
mimeType: MimeType.png,
);
},
);
Why This Change? #
The old file approach required platform-specific code and different methods for web vs. native platforms. The new unified download approach with callbacks provides:
- Single API for all platforms
- Consistent behavior across web, mobile, and desktop
- More flexibility with platform-specific implementations
- Better separation of concerns (package focuses on capture, callbacks handle platform details)
License #
MIT License - feel free to use this package in your projects.
See LICENSE file for details.
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
Repository #
https://github.com/SoundSliced/s_screenshot
Support #
For issues, questions, or feature requests, please open an issue on GitHub.