@@ -16,6 +16,7 @@ import 'android_proxy.dart';
16
16
import 'android_webview.dart' as android_webview;
17
17
import 'android_webview.dart' ;
18
18
import 'instance_manager.dart' ;
19
+ import 'platform_views_service_proxy.dart' ;
19
20
import 'weak_reference_utils.dart' ;
20
21
21
22
/// Object specifying creation parameters for creating a [AndroidWebViewController] .
@@ -369,20 +370,28 @@ class AndroidWebViewWidgetCreationParams
369
370
required super .controller,
370
371
super .layoutDirection,
371
372
super .gestureRecognizers,
373
+ this .displayWithHybridComposition = false ,
372
374
@visibleForTesting InstanceManager ? instanceManager,
375
+ @visibleForTesting
376
+ this .platformViewsServiceProxy = const PlatformViewsServiceProxy (),
373
377
}) : instanceManager = instanceManager ?? JavaObject .globalInstanceManager;
374
378
375
379
/// Constructs a [WebKitWebViewWidgetCreationParams] using a
376
380
/// [PlatformWebViewWidgetCreationParams] .
377
381
AndroidWebViewWidgetCreationParams .fromPlatformWebViewWidgetCreationParams (
378
382
PlatformWebViewWidgetCreationParams params, {
379
- InstanceManager ? instanceManager,
383
+ bool displayWithHybridComposition = false ,
384
+ @visibleForTesting InstanceManager ? instanceManager,
385
+ @visibleForTesting PlatformViewsServiceProxy platformViewsServiceProxy =
386
+ const PlatformViewsServiceProxy (),
380
387
}) : this (
381
388
key: params.key,
382
389
controller: params.controller,
383
390
layoutDirection: params.layoutDirection,
384
391
gestureRecognizers: params.gestureRecognizers,
392
+ displayWithHybridComposition: displayWithHybridComposition,
385
393
instanceManager: instanceManager,
394
+ platformViewsServiceProxy: platformViewsServiceProxy,
386
395
);
387
396
388
397
/// Maintains instances used to communicate with the native objects they
@@ -392,6 +401,25 @@ class AndroidWebViewWidgetCreationParams
392
401
/// outside of tests.
393
402
@visibleForTesting
394
403
final InstanceManager instanceManager;
404
+
405
+ /// Proxy that provides access to the platform views service.
406
+ ///
407
+ /// This service allows creating and controlling platform-specific views.
408
+ @visibleForTesting
409
+ final PlatformViewsServiceProxy platformViewsServiceProxy;
410
+
411
+ /// Whether the [WebView] will be displayed using the Hybrid Composition
412
+ /// PlatformView implementation.
413
+ ///
414
+ /// For most use cases, this flag should be set to false. Hybrid Composition
415
+ /// can have performance costs but doesn't have the limitation of rendering to
416
+ /// an Android SurfaceTexture. See
417
+ /// * https://flutter.dev/docs/development/platform-integration/platform-views#performance
418
+ /// * https://github.com/flutter/flutter/issues/104889
419
+ /// * https://github.com/flutter/flutter/issues/116954
420
+ ///
421
+ /// Defaults to false.
422
+ final bool displayWithHybridComposition;
395
423
}
396
424
397
425
/// An implementation of [PlatformWebViewWidget] with the Android WebView API.
@@ -411,30 +439,52 @@ class AndroidWebViewWidget extends PlatformWebViewWidget {
411
439
@override
412
440
Widget build (BuildContext context) {
413
441
return PlatformViewLink (
414
- key: _androidParams.key,
442
+ key: _androidParams.key,
443
+ viewType: 'plugins.flutter.io/webview' ,
444
+ surfaceFactory: (
445
+ BuildContext context,
446
+ PlatformViewController controller,
447
+ ) {
448
+ return AndroidViewSurface (
449
+ controller: controller as AndroidViewController ,
450
+ gestureRecognizers: _androidParams.gestureRecognizers,
451
+ hitTestBehavior: PlatformViewHitTestBehavior .opaque,
452
+ );
453
+ },
454
+ onCreatePlatformView: (PlatformViewCreationParams params) {
455
+ return _initAndroidView (
456
+ params,
457
+ displayWithHybridComposition:
458
+ _androidParams.displayWithHybridComposition,
459
+ )
460
+ ..addOnPlatformViewCreatedListener (params.onPlatformViewCreated)
461
+ ..create ();
462
+ },
463
+ );
464
+ }
465
+
466
+ AndroidViewController _initAndroidView (
467
+ PlatformViewCreationParams params, {
468
+ required bool displayWithHybridComposition,
469
+ }) {
470
+ if (displayWithHybridComposition) {
471
+ return _androidParams.platformViewsServiceProxy.initExpensiveAndroidView (
472
+ id: params.id,
415
473
viewType: 'plugins.flutter.io/webview' ,
416
- surfaceFactory: (
417
- BuildContext context,
418
- PlatformViewController controller,
419
- ) {
420
- return AndroidViewSurface (
421
- controller: controller as AndroidViewController ,
422
- gestureRecognizers: _androidParams.gestureRecognizers,
423
- hitTestBehavior: PlatformViewHitTestBehavior .opaque,
424
- );
425
- },
426
- onCreatePlatformView: (PlatformViewCreationParams params) {
427
- return PlatformViewsService .initSurfaceAndroidView (
428
- id: params.id,
429
- viewType: 'plugins.flutter.io/webview' ,
430
- layoutDirection: _androidParams.layoutDirection,
431
- creationParams: _androidParams.instanceManager.getIdentifier (
432
- (_androidParams.controller as AndroidWebViewController )
433
- ._webView),
434
- creationParamsCodec: const StandardMessageCodec (),
435
- )
436
- ..addOnPlatformViewCreatedListener (params.onPlatformViewCreated)
437
- ..create ();
438
- });
474
+ layoutDirection: _androidParams.layoutDirection,
475
+ creationParams: _androidParams.instanceManager.getIdentifier (
476
+ (_androidParams.controller as AndroidWebViewController )._webView),
477
+ creationParamsCodec: const StandardMessageCodec (),
478
+ );
479
+ } else {
480
+ return _androidParams.platformViewsServiceProxy.initSurfaceAndroidView (
481
+ id: params.id,
482
+ viewType: 'plugins.flutter.io/webview' ,
483
+ layoutDirection: _androidParams.layoutDirection,
484
+ creationParams: _androidParams.instanceManager.getIdentifier (
485
+ (_androidParams.controller as AndroidWebViewController )._webView),
486
+ creationParamsCodec: const StandardMessageCodec (),
487
+ );
488
+ }
439
489
}
440
490
}
0 commit comments