build method

  1. @override
Widget build(
  1. BuildContext context
)

Builds a new WebView.

Returns a Widget tree that embeds the created web view.

Implementation

@override
Widget build(BuildContext context) {
  return HtmlElementView(
    key: params.key,
    onPlatformViewCreated: (id) {
      final web.HTMLIFrameElement iFrame =
          _controller._webWebViewParams.iFrame;
      late final web.EventListener? listener;

      onload() async {
        final htmlString = _controller._htmlString;
        final javaScripts = _controller._javaScripts;

        _controller._htmlString = null;
        _controller._javaScripts = null;

        if (htmlString != null) {
          /// Restore HTML
          await _controller.loadHtmlString(htmlString);
        }

        await _controller._injectJavaScript(_kJavaScriptInjection);

        if (javaScripts != null) {
          /// Restore JavaScript
          for (final javaScript in javaScripts) {
            await _controller.runJavaScript(javaScript);
          }
        }

        if (_initialized) {
          _controller._messageChannels.values.map(
            (channel) => _controller.addJavaScriptChannel(channel.params)
          );
        } else {
          _controller._messageChannels.values.forEach(
            _controller._connectMessageChannel,
          );
        }

        if (_controller._navigationDelegate != null) {
          _controller._navigationDelegate!._onPageFinished!(iFrame.src);
        }

        _initialized = true;
      }

      listener = () {
        iFrame.removeEventListener('load', listener);
        iFrame.addEventListener(
            'load',
            () {
              if (_controller._navigationDelegate != null) {
                _controller._navigationDelegate!._onPageFinished!(iFrame.src);
              }
            }.toJS);

        onload();
      }.toJS;

      iFrame.addEventListener('load', listener);
      iFrame.src = 'about:blank';
    },
    viewType: _controller._webWebViewParams.iFrame.id,
  );
}