build method

  1. @override
Widget build({
  1. required BuildContext context,
  2. required CreationParams creationParams,
  3. required JavascriptChannelRegistry javascriptChannelRegistry,
  4. WebViewPlatformCreatedCallback? onWebViewPlatformCreated,
  5. Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers,
  6. required WebViewPlatformCallbacksHandler webViewPlatformCallbacksHandler,
})
override

Builds a new WebView.

Returns a Widget tree that embeds the created webview.

creationParams are the initial parameters used to setup the webview.

webViewPlatformHandler will be used for handling callbacks that are made by the created WebViewPlatformController.

onWebViewPlatformCreated will be invoked after the platform specific WebViewPlatformController implementation is created with the WebViewPlatformController instance as a parameter.

gestureRecognizers specifies which gestures should be consumed by the web view. It is possible for other gesture recognizers to be competing with the web view on pointer events, e.g if the web view is inside a ListView the ListView will want to handle vertical drags. The web view will claim gestures that are recognized by any of the recognizers on this list. When gestureRecognizers is empty or null, the web view will only handle pointer events for gestures that were not claimed by any other gesture recognizer.

webViewPlatformHandler must not be null.

Implementation

@override
Widget build({
  required BuildContext context,
  required CreationParams creationParams,
  required JavascriptChannelRegistry javascriptChannelRegistry,
  WebViewPlatformCreatedCallback? onWebViewPlatformCreated,
  Set<Factory<OneSequenceGestureRecognizer>>? gestureRecognizers,
  required WebViewPlatformCallbacksHandler webViewPlatformCallbacksHandler,
}) {
  return WebViewAndroidWidget(
    useHybridComposition: true,
    creationParams: creationParams,
    callbacksHandler: webViewPlatformCallbacksHandler,
    javascriptChannelRegistry: javascriptChannelRegistry,
    onBuildWidget: (WebViewAndroidPlatformController controller) {
      return PlatformViewLink(
        viewType: 'plugins.flutter.io/webview',
        surfaceFactory: (
          BuildContext context,
          PlatformViewController controller,
        ) {
          return AndroidViewSurface(
            controller: controller as AndroidViewController,
            gestureRecognizers: gestureRecognizers ??
                const <Factory<OneSequenceGestureRecognizer>>{},
            hitTestBehavior: PlatformViewHitTestBehavior.opaque,
          );
        },
        onCreatePlatformView: (PlatformViewCreationParams params) {
          final Color? backgroundColor = creationParams.backgroundColor;
          return _createViewController(
            // On some Android devices, transparent backgrounds can cause
            // rendering issues on the non hybrid composition
            // AndroidViewSurface. This switches the WebView to Hybrid
            // Composition when the background color is not 100% opaque.
            hybridComposition:
                backgroundColor != null && backgroundColor.opacity < 1.0,
            id: params.id,
            viewType: 'plugins.flutter.io/webview',
            // WebView content is not affected by the Android view's layout direction,
            // we explicitly set it here so that the widget doesn't require an ambient
            // directionality.
            layoutDirection:
                Directionality.maybeOf(context) ?? TextDirection.ltr,
            webViewIdentifier: JavaObject.globalInstanceManager
                .getIdentifier(controller.webView)!,
          )
            ..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
            ..addOnPlatformViewCreatedListener((int id) {
              if (onWebViewPlatformCreated != null) {
                onWebViewPlatformCreated(controller);
              }
            })
            ..create();
        },
      );
    },
  );
}