HighchartsView constructor

HighchartsView({
  1. Key? key,
  2. String? body,
  3. bool debug = false,
  4. String? foot,
  5. String? head,
  6. bool keepAlive = true,
  7. Widget onError(
    1. HighchartsView,
    2. Object?
    )?,
  8. dynamic onLoaded(
    1. HighchartsView
    )?,
  9. Widget onLoading(
    1. HighchartsView
    )?,
  10. void onMounted(
    1. HighchartsView
    )?,
})

Implementation

HighchartsView({
  super.key,
  this.body,
  this.debug = false,
  this.foot,
  this.head,
  this.keepAlive = true,
  this.onError,
  this.onLoaded,
  this.onLoading,
  this.onMounted,
}) {
  PlatformWebViewControllerCreationParams params;

  if (WebViewPlatform.instance is AndroidWebViewPlatform) {
    params = AndroidWebViewControllerCreationParams();
  } else if (WebViewPlatform.instance is WebKitWebViewPlatform) {
    params = WebKitWebViewControllerCreationParams();
  } else {
    params = const PlatformWebViewControllerCreationParams();
  }

  webViewController = WebViewController.fromPlatformCreationParams(params);

  if (webViewController.platform is WebKitWebViewController) {
    (webViewController.platform as WebKitWebViewController)
        .setInspectable(true);
  }

  if (webViewController.platform is AndroidWebViewController ||
      webViewController.platform is WebKitWebViewController) {
    webViewController
      ..setBackgroundColor(const Color(0x00000000))
      ..setJavaScriptMode(JavaScriptMode.unrestricted);
  }

  webViewController
    ..setNavigationDelegate(NavigationDelegate(onPageFinished: (_) {
      if (onMounted != null) {
        onMounted!(this);
      }
    }))
    ..addJavaScriptChannel(
      'highcharts_flutter_channel',
      onMessageReceived: (e) async {
        final dataIndex = e.message.indexOf('\n');

        final callbackKey =
            (dataIndex < 0 ? e.message : e.message.substring(0, dataIndex));
        final data = (dataIndex < 0
            ? []
            : (jsonDecode(e.message.substring(dataIndex + 1)) ?? []));

        if (callbackKey == 'highcharts_flutter.chart') {
          if (onLoaded != null) {
            onLoaded!(this);
          }
          return;
        }

        if (HighchartsCallback.registry.containsKey(callbackKey)) {
          HighchartsCallback.registry[callbackKey]!(data);
          return;
        }

        if (_events.containsKey(callbackKey)) {
          for (final callback in _events[callbackKey]!) {
            await callback(data);
          }
          return;
        }

        if (debug) {
          debugPrint('Unhandled callback: ${e.message}');
        }
      },
    );

  if (WebViewPlatform.instance is AndroidWebViewPlatform) {
    webView = WebViewWidget.fromPlatformCreationParams(
      params: AndroidWebViewWidgetCreationParams(
        controller: webViewController.platform,
        displayWithHybridComposition: true, // Fix scrolling interference
      ),
    );
  } else {
    webView = WebViewWidget(controller: webViewController);
  }
}