Skip to content

Commit ae5b66b

Browse files
committed
feat: updated screenshot manager
1 parent 9e167e6 commit ae5b66b

File tree

2 files changed

+50
-36
lines changed

2 files changed

+50
-36
lines changed

example/lib/screenshot_example.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ class _MyAppState extends State<MyApp> {
5757
Widget build(BuildContext context) {
5858
return MaterialApp(
5959
navigatorKey: Catcher.navigatorKey,
60-
home: CatcherScreenshot(
61-
catcher: Catcher.getInstance(),
62-
child: Scaffold(
63-
appBar: AppBar(
64-
title: const Text('Plugin example app'),
65-
),
66-
body: const ChildWidget(),
60+
home: Scaffold(
61+
appBar: AppBar(
62+
title: const Text('Plugin example app'),
63+
),
64+
body: CatcherScreenshot(
65+
catcher: Catcher.getInstance(),
66+
child: const ChildWidget(),
6767
),
6868
),
6969
);

lib/core/catcher_screenshot_manager.dart

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,46 +55,60 @@ class CatcherScreenshotManager {
5555

5656
Future<Uint8List?> _capture({
5757
double? pixelRatio,
58-
Duration? delay = const Duration(milliseconds: 20),
58+
Duration delay = const Duration(milliseconds: 20),
5959
}) {
60-
return Future.delayed(delay ?? const Duration(milliseconds: 20), () async {
61-
final image = await _captureAsUiImage(
62-
delay: Duration.zero,
63-
pixelRatio: pixelRatio,
64-
);
65-
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
66-
final pngBytes = byteData?.buffer.asUint8List();
60+
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
61+
return Future.delayed(delay, () async {
62+
try {
63+
final image = await captureAsUiImage(
64+
delay: Duration.zero,
65+
pixelRatio: pixelRatio,
66+
);
67+
final byteData =
68+
await image?.toByteData(format: ui.ImageByteFormat.png);
69+
image?.dispose();
70+
71+
final pngBytes = byteData?.buffer.asUint8List();
6772

68-
return pngBytes;
73+
return pngBytes;
74+
} catch (exception) {
75+
_logger.severe('Failed to capture screenshot: $exception');
76+
}
77+
return null;
6978
});
7079
}
7180

72-
Future<ui.Image> _captureAsUiImage({
73-
double? pixelRatio,
74-
Duration delay = const Duration(milliseconds: 100),
81+
Future<ui.Image?> captureAsUiImage({
82+
double? pixelRatio = 1,
83+
Duration delay = const Duration(milliseconds: 20),
7584
}) {
85+
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
7686
return Future.delayed(delay, () async {
77-
// ignore: cast_nullable_to_non_nullable
78-
79-
final renderObject = _containerKey.currentContext?.findRenderObject();
87+
try {
88+
final findRenderObject =
89+
_containerKey.currentContext?.findRenderObject();
8090

81-
// ignore: unnecessary_null_comparison
82-
if (renderObject == null) {
83-
throw StateError('No boundary found');
84-
}
85-
86-
final boundary = renderObject as RenderRepaintBoundary;
91+
print(containerKey.currentContext);
92+
print( _containerKey.currentContext?.findRenderObject());
93+
if (findRenderObject == null) {
94+
return null;
95+
}
8796

88-
final context = _containerKey.currentContext;
89-
var pixelRatioValue = pixelRatio;
90-
if (pixelRatioValue == null) {
91-
if (context != null) {
92-
pixelRatioValue =
93-
pixelRatioValue ?? MediaQuery.of(context).devicePixelRatio;
97+
final boundary = findRenderObject as RenderRepaintBoundary;
98+
final context = _containerKey.currentContext;
99+
var pixelRatioValue = pixelRatio;
100+
if (pixelRatio == null) {
101+
if (context != null) {
102+
pixelRatioValue =
103+
pixelRatio ?? MediaQuery.of(context).devicePixelRatio;
104+
}
94105
}
106+
final image = await boundary.toImage(pixelRatio: pixelRatioValue ?? 1);
107+
return image;
108+
} catch (exception) {
109+
_logger.severe('Failed to capture screenshot: $exception');
95110
}
96-
final image = await boundary.toImage(pixelRatio: pixelRatio ?? 1);
97-
return image;
111+
return null;
98112
});
99113
}
100114

0 commit comments

Comments
 (0)