Sub topic 3
Deciding who receives touches
This chapter assumes you know Flutter widgets, have used a MethodChannel, can read simple Kotlin and Swift, and can run an app on a device or simulator.
Deciding who receives touches
Flutter's gesture arena picks one recognizer for each touch. A scrollable Flutter parent often wins a drag before the native view sees it. To let the native view claim touches in its area, pass an EagerGestureRecognizer:
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
class EagerNativeView extends StatelessWidget {
const EagerNativeView({super.key});
@override
Widget build(BuildContext context) {
return UiKitView(
viewType: 'com.example.app/native_label',
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{
Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()),
},
);
}
}Cleaning up when the view goes away
Flutter disposes the platform view when its widget leaves the tree. On Android, PlatformView.dispose() must remove listeners and channel handlers. On iOS, remove observers and timers in the view's deinit. Skipping this step keeps native objects alive and leaks memory.
Android composition modes and PlatformViewLink
AndroidView uses virtual display mode, which is simple but copies frames and handles text input and accessibility less reliably. PlatformViewLink with PlatformViewsService.initSurfaceAndroidView uses hybrid composition. The native view joins the real Android view hierarchy, which improves input and accessibility at a higher CPU and memory cost.
Performance costs and known limitations
Each platform view costs more memory and CPU than a Flutter widget. Long lists of native views scroll slowly. Native views also need separate work to support accessibility and theming.
Alternatives and shipping as a plugin
Check whether a pure Flutter widget or a Texture can do the job first. A Texture shows frames rendered natively without a native view in the hierarchy. To reuse a platform view across apps, move the factory and registration into a Flutter plugin, with native code in its Android and iOS folders.
| Option | Best for | Main cost |
|---|---|---|
| Pure Flutter widget | Standard UI | Must reimplement native behavior |
| Texture | Rendered frames, such as video | Native code handles rendering and input |
| Platform view | Native controls needing full fidelity | Memory, CPU, and gesture complexity |
Exercise
A ListView places a platform view inside it, and the list keeps stealing drags. What causes this, and how do you fix it?
Show answer
The ListView's drag recognizer wins the gesture arena first. Give the platform view an EagerGestureRecognizer in its gestureRecognizers so it claims the touch.