mount static method

OffscreenWidget mount(
  1. Widget widget, {
  2. required Size size,
})

Implementation

static OffscreenWidget mount(Widget widget, {required Size size}) {
  var binding = WidgetsFlutterBinding.ensureInitialized();
  var view = binding.platformDispatcher.implicitView;
  if (view == null) {
    throw StateError('offscreen mounting needs an implicit FlutterView');
  }
  var boundary = RenderRepaintBoundary();
  var renderView = RenderView(
    view: view,
    configuration: ViewConfiguration(
      logicalConstraints: BoxConstraints.tight(size),
      physicalConstraints: BoxConstraints.tight(size),
    ),
    child: boundary,
  );
  var pipelineOwner = PipelineOwner();
  var buildOwner = BuildOwner(focusManager: FocusManager());
  pipelineOwner.rootNode = renderView;
  renderView.prepareInitialFrame();
  // The initial build happens inside attachToRenderTree itself, so the
  // error hook has to be up before it — not only around pump().
  var mountErrors = <FlutterErrorDetails>[];
  var previousOnError = FlutterError.onError;
  FlutterError.onError = mountErrors.add;
  RenderObjectToWidgetElement<RenderBox> element;
  try {
    element = RenderObjectToWidgetAdapter<RenderBox>(
      container: boundary,
      child: MediaQuery(
        data: MediaQueryData(size: size),
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: widget,
        ),
      ),
    ).attachToRenderTree(buildOwner);
  } finally {
    FlutterError.onError = previousOnError;
  }
  var mounted = OffscreenWidget._(
    buildOwner,
    pipelineOwner,
    renderView,
    element,
    boundary,
  );
  mounted.flutterErrors.addAll(mountErrors);
  mounted.pump();
  return mounted;
}