@@ -14,6 +14,7 @@ import 'src/platform_specific/file_manager/file_manager.dart';
14
14
class Screenshot <T > extends StatefulWidget {
15
15
final Widget ? child;
16
16
final ScreenshotController controller;
17
+
17
18
const Screenshot ({
18
19
Key ? key,
19
20
required this .child,
@@ -33,6 +34,7 @@ class Screenshot<T> extends StatefulWidget {
33
34
///
34
35
class ScreenshotController {
35
36
late GlobalKey _containerKey;
37
+
36
38
ScreenshotController () {
37
39
_containerKey = GlobalKey ();
38
40
}
@@ -50,6 +52,8 @@ class ScreenshotController {
50
52
);
51
53
ByteData ? byteData =
52
54
await image? .toByteData (format: ui.ImageByteFormat .png);
55
+ image? .dispose ();
56
+
53
57
Uint8List ? pngBytes = byteData? .buffer.asUint8List ();
54
58
55
59
return pngBytes;
@@ -75,9 +79,10 @@ class ScreenshotController {
75
79
return fileManager.saveFile (content! , directory, name: fileName);
76
80
}
77
81
78
- Future <ui.Image ?> captureAsUiImage (
79
- {double ? pixelRatio: 1 ,
80
- Duration delay: const Duration (milliseconds: 20 )}) {
82
+ Future <ui.Image ?> captureAsUiImage ({
83
+ double ? pixelRatio = 1 ,
84
+ Duration delay = const Duration (milliseconds: 20 ),
85
+ }) {
81
86
//Delay is required. See Issue https://github.com/flutter/flutter/issues/22308
82
87
return new Future .delayed (delay, () async {
83
88
try {
@@ -110,7 +115,7 @@ class ScreenshotController {
110
115
///
111
116
Future <Uint8List > captureFromWidget (
112
117
Widget widget, {
113
- Duration delay: const Duration (seconds: 1 ),
118
+ Duration delay = const Duration (seconds: 1 ),
114
119
double ? pixelRatio,
115
120
BuildContext ? context,
116
121
Size ? targetSize,
@@ -122,13 +127,15 @@ class ScreenshotController {
122
127
targetSize: targetSize);
123
128
final ByteData ? byteData =
124
129
await image.toByteData (format: ui.ImageByteFormat .png);
130
+ image.dispose ();
125
131
126
132
return byteData! .buffer.asUint8List ();
127
133
}
128
134
135
+ /// If you are building a desktop/web application that supports multiple view. Consider passing the [context] so that flutter know which view to capture.
129
136
static Future <ui.Image > widgetToUiImage (
130
137
Widget widget, {
131
- Duration delay: const Duration (seconds: 1 ),
138
+ Duration delay = const Duration (seconds: 1 ),
132
139
double ? pixelRatio,
133
140
BuildContext ? context,
134
141
Size ? targetSize,
@@ -159,16 +166,20 @@ class ScreenshotController {
159
166
160
167
final RenderRepaintBoundary repaintBoundary = RenderRepaintBoundary ();
161
168
162
- Size logicalSize = targetSize ??
163
- ui.window.physicalSize / ui.window.devicePixelRatio; // Adapted
164
- Size imageSize = targetSize ?? ui.window.physicalSize; // Adapted
169
+ final platformDispatcher = WidgetsBinding .instance.platformDispatcher;
170
+ final fallBackView = platformDispatcher.views.first;
171
+ final view =
172
+ context == null ? fallBackView : View .maybeOf (context) ?? fallBackView;
173
+ Size logicalSize =
174
+ targetSize ?? view.physicalSize / view.devicePixelRatio; // Adapted
175
+ Size imageSize = targetSize ?? view.physicalSize; // Adapted
165
176
166
177
assert (logicalSize.aspectRatio.toStringAsPrecision (5 ) ==
167
178
imageSize.aspectRatio
168
179
.toStringAsPrecision (5 )); // Adapted (toPrecision was not available)
169
180
170
181
final RenderView renderView = RenderView (
171
- window : ui.window ,
182
+ view : view ,
172
183
child: RenderPositionedBox (
173
184
alignment: Alignment .center, child: repaintBoundary),
174
185
configuration: ViewConfiguration (
@@ -226,7 +237,7 @@ class ScreenshotController {
226
237
pixelRatio: pixelRatio ?? (imageSize.width / logicalSize.width));
227
238
228
239
///
229
- ///This delay sholud increas with Widget tree Size
240
+ ///This delay should increase with Widget tree Size
230
241
///
231
242
232
243
await Future .delayed (delay);
@@ -251,10 +262,16 @@ class ScreenshotController {
251
262
retryCounter-- ;
252
263
253
264
///
254
- ///retry untill capture is successfull
265
+ /// retry until capture is successful
255
266
///
256
-
257
267
} while (isDirty && retryCounter >= 0 );
268
+ try {
269
+ /// Dispose All widgets
270
+ // rootElement.visitChildren((Element element) {
271
+ // rootElement.deactivateChild(element);
272
+ // });
273
+ buildOwner.finalizeTree ();
274
+ } catch (e) {}
258
275
259
276
return image; // Adapted to directly return the image and not the Uint8List
260
277
}
0 commit comments