Sub topic 10
Android Composition Modes and PlatformViewLink
Assume you know basic Flutter widgets, Dart, and platform views. You should also be able to read simple Kotlin and understand that Android views live in the native Android view hierarchy.
A Flutter platform view lets an Android View, such as a map, video player, or WebView, appear inside the Flutter widget tree. Android must combine two rendering systems: Flutter draws its scene, while Android draws the embedded view. The composition mode controls how that combination happens.
Two Android composition modes
Flutter provides two main approaches.
| Mode | Main idea | Typical strength | Common cost |
|---|---|---|---|
| Hybrid composition | The native view is attached directly to the Android view hierarchy | Strong compatibility with native behavior | Can affect Flutter rendering performance |
| Texture-layer hybrid composition | The native view is rendered through a texture layer used by Flutter | Better control over Flutter composition | Some native surfaces and behaviors have limitations |
With direct hybrid composition, Android owns the native view in its normal hierarchy. This usually gives the view good compatibility with Android features such as focus, accessibility, and overlays. However, switching between Flutter rendering and Android rendering can increase frame cost, especially when the view is large, animated, or placed in a scrolling list.
Texture-layer hybrid composition allows Flutter to place the native content in its layer tree. This can reduce some composition problems and gives Flutter more control over clipping and transforms. It may not work equally well with every native surface. Video surfaces, complex animations, and certain system UI effects need testing on real devices.
Using PlatformViewLink
PlatformViewLink is a lower-level widget for creating an Android platform view. It separates two jobs:
onCreatePlatformViewcreates and configures the native controller.surfaceFactorycreates the Flutter-side surface that displays the controller.
The following example uses initSurfaceAndroidView, which creates a texture-layer hybrid composition view. The view type must match the type registered by the Android PlatformViewFactory.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
class HybridCompositionLabel extends StatelessWidget {
const HybridCompositionLabel({super.key});
static const String viewType = 'com.example.app/native_label';
@override
Widget build(BuildContext context) {
final creationParams = <String, dynamic>{
'text': 'Hybrid composition',
};
return SizedBox(
height: 80,
child: PlatformViewLink(
viewType: viewType,
surfaceFactory: (context, controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers:
const <Factory<OneSequenceGestureRecognizer>>{},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: viewType,
layoutDirection: TextDirection.ltr,
creationParams: creationParams,
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
)
..addOnPlatformViewCreatedListener(
params.onPlatformViewCreated,
)
..create();
},
),
);
}
}The empty gestureRecognizers set means the platform surface can receive pointer events according to the platform-view hit-testing rules. If Flutter recognizers also need to compete for the same gestures, add factories for the required recognizer types. Be deliberate: accepting every gesture on the native view can prevent a surrounding Flutter ListView from scrolling.
The controller ID is assigned by Flutter. The listener reports that the native view has been created. Use this callback when you need to open a per-view channel or update state that depends on the native instance.
Choosing a mode
Start with the simpler AndroidView widget when it provides the behavior you need. Use PlatformViewLink when you need explicit control over the Android surface and controller setup.
Test these cases before choosing:
- Scrolling around the view.
- Clipping, opacity, transforms, and overlays.
- Touch gestures shared with Flutter parents.
- Keyboard focus and accessibility.
- Video or other hardware-accelerated surfaces.
- Several instances visible at the same time.
Exercise
Why might PlatformViewLink be preferred over AndroidView for an embedded video or a view inside a complex Flutter layout?
Show answer
PlatformViewLink gives explicit control over the Android surface and controller, allowing the composition behavior and gesture handling to be tuned and tested for that native view.