Sub topic 11
Performance Costs and Known Limitations of Flutter Platform Views
This chapter assumes that you know basic Flutter widgets, Dart, and platform channels. You should also be able to read simple Kotlin and Swift. The examples assume that you can run an app on an Android or iOS device.
A platform view places a real Android View or iOS UIView inside Flutter's widget tree. Flutter still controls the surrounding layout, but the native view is rendered and receives input through a separate platform system.
That boundary is useful, but it has a cost.
Where the cost comes from
Flutter normally paints its widgets in one rendering pipeline. A platform view must connect that pipeline to the native platform's view hierarchy.
The bridge can add work in several places:
- Flutter must position and size the native view.
- The operating system must composite Flutter pixels and native pixels.
- Touch events must be routed between Flutter recognizers and the native view.
- Focus, accessibility, and text input must cross framework boundaries.
- A scrolling list may create, move, or dispose many native views.
A single small platform view is often fine. Problems become more likely when many views appear on screen, when a view updates frequently, or when the view contains expensive native rendering.
Android composition choices
Android supports more than one way to compose a platform view. The choice affects fidelity, speed, and limitations.
Hybrid composition adds the Android view to the real Android view hierarchy. It usually provides the best compatibility with native behavior, including accessibility, focus, overlays, and controls that rely on the window hierarchy. The trade-off is extra composition work. Scrolling and animation can become less smooth, especially on older devices or when several native views are visible.
Texture-layer composition renders the Android view into a texture that Flutter can place in its scene. This can integrate more naturally with Flutter transforms, clipping, and animations. However, some Android views, especially views backed by a SurfaceView or other external surface, may not work correctly. Native controls that expect to be directly attached to the window can also behave differently.
The exact API depends on the Flutter version. For example, PlatformViewLink with AndroidViewSurface creates a surface-based Android platform view:
return PlatformViewLink(
viewType: 'com.example.app/native_label',
surfaceFactory: (context, controller) {
return AndroidViewSurface(
controller: controller as AndroidViewController,
gestureRecognizers: const {},
hitTestBehavior: PlatformViewHitTestBehavior.opaque,
);
},
onCreatePlatformView: (params) {
return PlatformViewsService.initSurfaceAndroidView(
id: params.id,
viewType: 'com.example.app/native_label',
layoutDirection: TextDirection.ltr,
creationParams: {'text': 'Native content'},
creationParamsCodec: const StandardMessageCodec(),
onFocus: () => params.onFocusChanged(true),
)
..addOnPlatformViewCreatedListener(params.onPlatformViewCreated)
..create();
},
);Use the composition mode that matches the native view. Test on physical devices rather than relying only on an emulator.
iOS and cross-platform limitations
UIKit views also live outside Flutter's normal rendering path. Complex clipping, opacity, transforms, and animations can cost more than the equivalent Flutter widget. A native view may not behave exactly like a Flutter child when it overlaps other content.
Platform views also have practical limitations:
- They are difficult to use in large, fast-scrolling lists.
- A view that draws continuously can consume significant CPU or GPU time.
- Frequent Dart-to-native messages can cause jank if each message triggers layout or rendering.
- Gesture conflicts can make scrolling, dragging, and tapping feel inconsistent.
- Keyboard focus and accessibility need testing on both platforms.
- Native views can have different fonts, padding, shadows, and semantics from Flutter widgets.
- Screenshots, tests, and golden tests may need platform-specific handling.
- A native dependency can increase application size and platform maintenance work.
A platform view is not automatically slow. The cost depends on the view, its update rate, its size, and the number of instances.
How to decide
Use a platform view when the native SDK provides important behavior that would be costly or impossible to reproduce in Flutter. Examples include a map SDK, a web browser, a payment control, or a vendor-specific video surface.
Prefer a pure Flutter widget when the content is mostly ordinary text, shapes, images, or controls. Flutter usually gives more consistent layout, animation, theming, testing, and accessibility.
Consider a texture when the native component mainly produces pixels, such as a camera or video stream. A texture can avoid some view-hierarchy problems, but it does not automatically provide native controls, focus, or accessibility.
| Requirement | Usually suitable choice |
|---|---|
| Native interaction and accessibility are essential | Platform view |
| Many animated visual surfaces are visible | Texture or custom Flutter rendering |
| Standard controls and layout are enough | Pure Flutter widget |
Measure before and after integration. Profile frame times while scrolling, opening the screen, animating the view, and sending messages. Check memory use and test the oldest supported device.
Exercise
A screen contains ten continuously updating native video views in a Flutter scrolling list. What should you investigate first?
Show answer
Profile scrolling and frame times on a physical low-end device. Then compare fewer platform views, a texture-based solution, and a native or Flutter layout that avoids creating ten independently composited views.