11import "dart:typed_data" show Uint8List;
2- import "dart:ui" show ImageByteFormat;
2+ import "dart:ui" show FlutterView, ImageByteFormat;
33
4+ import "package:flutter/material.dart" ;
45import "package:flutter/rendering.dart"
56 show
67 PipelineOwner,
78 RenderPositionedBox,
89 RenderRepaintBoundary,
910 RenderView,
1011 ViewConfiguration;
11- import "package:flutter/widgets.dart" ;
1212
1313class WidgetToImageUtil {
14- static Widget _setSizeAndTextDirection (Widget widget, Size size) {
14+ static Widget _setSizeAndTextDirection (
15+ Widget widget, Size size, BuildContext context, FlutterView view) {
1516 return SizedBox (
1617 width: size.width,
1718 height: size.height,
18- child: Directionality (
19- textDirection: TextDirection .ltr,
20- child: widget,
21- ),
19+ child: MediaQuery (
20+ data: MediaQueryData .fromView (view),
21+ child: Theme (
22+ data: Theme .of (context),
23+ child: Directionality (
24+ textDirection: TextDirection .ltr,
25+ child: widget,
26+ ))),
2227 );
2328 }
2429
@@ -29,33 +34,52 @@ class WidgetToImageUtil {
2934 }) async {
3035 final renderBox = RenderRepaintBoundary ();
3136 final view = View .of (context);
37+
38+ final renderPositionedBox =
39+ RenderPositionedBox (alignment: Alignment .center, child: renderBox);
3240 final renderView = RenderView (
3341 view: view,
3442 configuration: ViewConfiguration (
3543 size: size, devicePixelRatio: view.devicePixelRatio),
36- child:
37- RenderPositionedBox (alignment: Alignment .center, child: renderBox));
44+ child: renderPositionedBox);
3845
3946 final pipelineOwner = PipelineOwner ()..rootNode = renderView;
4047 renderView.prepareInitialFrame ();
4148
4249 final buildOwner = BuildOwner (focusManager: FocusManager ());
43- final renderToWidget = RenderObjectToWidgetAdapter (
44- container: renderBox, child: _setSizeAndTextDirection (widget, size))
50+ final rootElement = RenderObjectToWidgetAdapter (
51+ container: renderBox,
52+ child: _setSizeAndTextDirection (widget, size, context, view))
4553 .attachToRenderTree (buildOwner);
4654 buildOwner
47- ..buildScope (renderToWidget )
55+ ..buildScope (rootElement )
4856 ..finalizeTree ();
4957
5058 pipelineOwner
5159 ..flushLayout ()
5260 ..flushCompositingBits ()
5361 ..flushPaint ();
62+ try {
63+ final image = await renderBox.toImage (pixelRatio: view.devicePixelRatio);
64+
65+ final rawImage = await image
66+ .toByteData (format: ImageByteFormat .png)
67+ .then ((b) => b! .buffer.asUint8List ());
5468
55- final image = await renderBox.toImage (pixelRatio: view.devicePixelRatio);
56- return image
57- .toByteData (format: ImageByteFormat .png)
58- .then ((b) => b! .buffer.asUint8List ());
69+ return rawImage;
70+ } finally {
71+ final emptyRenderToWidgetAdapter =
72+ RenderObjectToWidgetAdapter (container: renderBox);
73+ rootElement.update (emptyRenderToWidgetAdapter); // renderbox child = null
74+ buildOwner.finalizeTree ();
75+ renderView
76+ ..detach ()
77+ ..dispose ();
78+ rootElement
79+ ..detachRenderObject ()
80+ ..deactivate ();
81+ buildOwner.finalizeTree ();
82+ }
5983 }
6084
6185 WidgetToImageUtil ._();
0 commit comments