Sub topic 8
Deciding Who Receives Touches
Assume that you know basic Flutter widgets and MethodChannel, and that you can read simple Dart and native platform code. This chapter focuses on the boundary between Flutter gesture recognizers and an embedded native view.
A platform view is a native Android or iOS view placed inside Flutter's widget tree. Both Flutter and the native view may want the same touch. For example, a WebView may need to scroll while its Flutter parent is also a scrollable list. Gesture configuration decides which side wins.
How touch ownership works
Flutter first performs hit testing to find widgets under the pointer. A platform-view widget is included in that process. Flutter then uses gesture recognizers to decide which widget owns the gesture sequence.
A gesture sequence usually begins with a pointer-down event and continues through movement and pointer-up. Once a recognizer wins, the competing recognizers normally stop receiving that sequence.
For a platform view, gestureRecognizers controls which gestures Flutter is willing to give to the native view.
| Configuration | Typical result |
|---|---|
| Empty set | The native view receives the gesture only when no competing Flutter recognizer claims it |
| `EagerGestureRecognizer` | The native view claims the gesture immediately |
| A specific recognizer | The native view receives gestures matching that recognizer |
The default empty set is useful when a Flutter parent should have the first chance to handle gestures. It can also make an embedded native view feel unresponsive inside a scrollable parent.
Let the native view claim touches
Use EagerGestureRecognizer when the native view must receive touches, such as a map, camera preview, video surface, or web page. The recognizer claims the gesture without waiting for another recognizer to win.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class NativeCounterView extends StatelessWidget {
const NativeCounterView({super.key});
static const viewType = 'com.example.app/native_counter';
@override
Widget build(BuildContext context) {
return SizedBox(
height: 240,
child: UiKitView(
viewType: viewType,
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>{
Factory<OneSequenceGestureRecognizer>(
() => EagerGestureRecognizer(),
),
},
),
);
}
}This example uses UiKitView. Use the same gestureRecognizers configuration with AndroidView when the native implementation is on Android.
An eager recognizer has a cost. If the platform view claims every gesture, a surrounding Flutter ListView, PageView, or Dismissible may no longer receive swipes that begin over the native view.
Share gestures deliberately
Sometimes the native view should receive only a particular gesture. For example, you might allow a native view to handle taps while preserving vertical scrolling in a Flutter parent. Flutter's gesture arena makes this possible, but the exact result depends on the recognizers used by both widgets.
A practical approach is:
- Start with an empty recognizer set.
- Test taps, horizontal drags, and vertical drags.
- Add a specific recognizer only when the native view needs it.
- Test the view inside every parent that can compete for the gesture.
Do not add eager recognition just because it makes one interaction work. It may silently disable scrolling or page navigation around the view.
Hit testing and Android surfaces
When using the lower-level Android PlatformViewLink API, AndroidViewSurface also has a hitTestBehavior. With opaque, the platform view participates as a solid hit-test target. This is a good default when the native view should occupy its full rectangle.
The surface's hit-test behavior and its gesture recognizers solve different problems:
- Hit testing decides whether the platform view is considered at a location.
- Gesture recognizers decide which side owns the gesture after a pointer reaches that location.
Test gesture ownership
Check each important interaction on a real device or simulator. Emulators can differ from physical devices, especially for multi-touch, nested scrolling, maps, and web content.
Useful test cases include:
- Tap a control inside the native view.
- Drag vertically when the native view is inside a
ListView. - Drag horizontally when it is inside a
PageView. - Start a drag inside the native view and finish outside it.
- Rotate the device or rebuild the widget while a gesture is active.
- Test both platforms because Android and iOS composition paths differ.
Exercise
A native map is embedded inside a Flutter ListView. The map must support pinching and panning, but the list must still scroll when the user starts a vertical drag over the map. Should you immediately use EagerGestureRecognizer?
Show answer
No. Start with an empty recognizer set and test the gesture arena. If the map must receive all touch sequences, use an eager recognizer, but then the list may not scroll from that area. A more selective gesture design or a different layout may be required.