widget static method
Future<Screenshot>
widget(
- Widget child, {
- Size? size,
- double? pixelRatio,
- BuildContext? context,
- Duration delay = Duration.zero,
Captures child without showing it, e.g. a share card built just for
the image.
child is laid out within size, or the screen's size, and the PNG
is as big as child is: wrap it in a SizedBox for an exact size.
pixelRatio defaults to the device's. With context, child gets
that context's theme, text direction, localizations and media query;
otherwise it gets the app's current theme. delay gives images in
child time to load before the capture. ScreenshotHidden widgets in
child are hidden.
Implementation
static Future<Screenshot> widget(
Widget child, {
Size? size,
double? pixelRatio,
BuildContext? context,
Duration delay = Duration.zero,
}) async {
final ui.FlutterView view =
WidgetsBinding.instance.platformDispatcher.implicitView ??
WidgetsBinding.instance.platformDispatcher.views.first;
final double ratio = pixelRatio ?? view.devicePixelRatio;
final Size bounds = size ?? view.physicalSize / view.devicePixelRatio;
final Widget content = context == null
? MediaQuery(
data: MediaQueryData(size: bounds, devicePixelRatio: ratio),
child: Directionality(
textDirection: TextDirection.ltr,
child: Theme(
data: NyThemeManager.instance.themeData ?? ThemeData(),
child: Builder(
builder: (BuildContext context) => DefaultTextStyle(
style:
Theme.of(context).textTheme.bodyMedium ??
const TextStyle(),
child: child,
),
),
),
),
)
: InheritedTheme.captureAll(
context,
MediaQuery(
data: MediaQuery.of(context).copyWith(size: bounds),
child: Localizations.override(context: context, child: child),
),
);
final RenderRepaintBoundary boundary = RenderRepaintBoundary();
final RenderView renderView = RenderView(
view: view,
configuration: ViewConfiguration(
logicalConstraints: BoxConstraints.tight(bounds),
physicalConstraints: BoxConstraints.tight(bounds * ratio),
devicePixelRatio: ratio,
),
child: RenderPositionedBox(alignment: Alignment.topLeft, child: boundary),
);
final PipelineOwner pipelineOwner = PipelineOwner()..rootNode = renderView;
renderView.prepareInitialFrame();
final FocusManager focusManager = FocusManager();
final BuildOwner buildOwner = BuildOwner(focusManager: focusManager);
final RenderObjectToWidgetElement<RenderBox> root =
RenderObjectToWidgetAdapter<RenderBox>(
container: boundary,
child: _Offscreen(child: content),
).attachToRenderTree(buildOwner);
void draw() {
buildOwner.buildScope(root);
buildOwner.finalizeTree();
pipelineOwner.flushLayout();
pipelineOwner.flushCompositingBits();
pipelineOwner.flushPaint();
}
try {
draw();
if (delay > Duration.zero) {
await Future<void>.delayed(delay);
draw();
}
final ui.Image image = await boundary.toImage(pixelRatio: ratio);
return await _encode(image);
} finally {
// Unmount the tree so its states are disposed.
RenderObjectToWidgetAdapter<RenderBox>(container: boundary)
.attachToRenderTree(buildOwner, root);
buildOwner.buildScope(root);
buildOwner.finalizeTree();
focusManager.dispose();
}
}