Sub topic 8
Deciding who receives touches
This chapter assumes you can already show a native view with AndroidView or UiKitView, as covered earlier in this tutorial. It also assumes you know what a ListView and a GestureDetector are. You don't need to know Flutter's gesture internals, because the chapter introduces what you need.
Why touches need a decision
A platform view sits inside the Flutter widget tree, but its pixels and its touch handling belong to the native toolkit. When a finger lands on the screen, Flutter has to answer two questions:
- Which widget is under the finger? This is hit testing.
- If several recognizers want the same gesture, which one wins? This is the gesture arena.
A native view that sits alone on a screen rarely has a problem. The trouble starts when a Flutter ancestor also reacts to touches. A ListView listens for vertical drags, and a PageView listens for horizontal ones. A map inside a scrolling list needs the same drags to pan the map. Without configuration, the Flutter scrollable usually wins, and the native view never receives the drag.
The gesture arena in one paragraph
When a pointer goes down, every Flutter recognizer under it joins an arena. Each recognizer watches the movement and either claims the gesture or gives up. One recognizer wins, and the others are told to stop. A platform view takes part in this contest through a recognizer that Flutter builds for it. The native view gets the touch stream only if that recognizer wins, or if nothing else competes for the gesture.
The gestureRecognizers parameter of AndroidView and UiKitView controls how that recognizer behaves. It is a set of Factory objects. Each factory creates one recognizer that competes in the arena on behalf of the native view.
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
final Set<Factory<OneSequenceGestureRecognizer>> nativeViewGestures =
<Factory<OneSequenceGestureRecognizer>>{
Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()),
};Read the set as "these are the gestures the native view wants to win". It does not mean "only these gestures reach the native view". The set matters mainly when a Flutter recognizer competes for the same gesture.
Choosing recognizers
The recognizer you list decides which gestures the native view takes away from Flutter ancestors.
| Recognizer | The native view wins | Typical use |
|---|---|---|
| `EagerGestureRecognizer` | Every gesture that starts on the view, immediately | Maps, canvases, video players with their own controls |
| `VerticalDragGestureRecognizer` | Vertical drags | A vertically scrolling native list inside a Flutter `ListView` |
| `HorizontalDragGestureRecognizer` | Horizontal drags | A native carousel inside a `PageView` |
| `ScaleGestureRecognizer` | Pinch and pan with one or more fingers | Zoomable native content |
| `TapGestureRecognizer` | Taps | A native button inside a tappable Flutter parent |
| Empty set | Nothing extra. Flutter ancestors win any contested gesture. | Static native content such as a label |
EagerGestureRecognizer is the blunt option. It declares victory as soon as the pointer goes down, so no Flutter ancestor can claim a drag that starts on the native view. That is what you want for a map. It also means the user cannot scroll the surrounding list by dragging on the map. Users usually accept this, but you should know it is a consequence.
A worked example: a native view in a scrolling list
The following screen places a native view between two Flutter items in a ListView. The first version uses an empty set. The second uses EagerGestureRecognizer. It assumes a view type named com.example.app/native_counter is registered on both platforms, as in the earlier chapters.
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
const String counterViewType = 'com.example.app/native_counter';
class NativeCounterView extends StatelessWidget {
const NativeCounterView({super.key, required this.claimGestures});
final bool claimGestures;
static final Set<Factory<OneSequenceGestureRecognizer>> _eager =
<Factory<OneSequenceGestureRecognizer>>{
Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()),
};
@override
Widget build(BuildContext context) {
final recognizers = claimGestures
? _eager
: const <Factory<OneSequenceGestureRecognizer>>{};
switch (defaultTargetPlatform) {
case TargetPlatform.android:
return AndroidView(
viewType: counterViewType,
layoutDirection: TextDirection.ltr,
creationParamsCodec: const StandardMessageCodec(),
gestureRecognizers: recognizers,
);
case TargetPlatform.iOS:
return UiKitView(
viewType: counterViewType,
layoutDirection: TextDirection.ltr,
creationParamsCodec: const StandardMessageCodec(),
gestureRecognizers: recognizers,
);
default:
return const Center(child: Text('Native view not supported here'));
}
}
}
class GestureDemo extends StatefulWidget {
const GestureDemo({super.key});
@override
State<GestureDemo> createState() => _GestureDemoState();
}
class _GestureDemoState extends State<GestureDemo> {
bool _claim = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Touch ownership'),
actions: [
Switch(value: _claim, onChanged: (v) => setState(() => _claim = v)),
],
),
body: ListView(
children: [
for (var i = 0; i < 5; i++) ListTile(title: Text('Item $i')),
SizedBox(height: 240, child: NativeCounterView(claimGestures: _claim)),
for (var i = 5; i < 20; i++) ListTile(title: Text('Item $i')),
],
),
);
}
}Run it and drag inside the native view with the switch on and off. With the switch off, the list scrolls and the native view sees little of the drag. With it on, a drag that begins on the native view stays inside the native view, and the list scrolls only when you drag outside it.
The recognizer set is a static final field so it is created once and not on every build. The framework compares the set contents when it updates the render object, so a stable set avoids unnecessary work.
Following one touch through the layers
The diagram shows the path of a single touch that lands on a native view inside a scrolling list.
Hit testing: who is under the finger
Before the arena runs, Flutter hit tests the widget tree. Both AndroidView and UiKitView accept a hitTestBehavior argument of type PlatformViewHitTestBehavior. It decides how the view behaves when other widgets overlap it.
| Value | The native view receives touches | Widgets behind it receive touches |
|---|---|---|
| `opaque` (default) | Yes | No |
| `translucent` | Yes | Yes |
| `transparent` | No | Yes |
Use opaque for ordinary cases. Use transparent when the native view is decoration, such as a native video layer under Flutter controls that should handle every tap. Use translucent when both layers need the same tap, which is rare.
Overlays are a common source of confusion. A Flutter widget placed above a native view in a Stack is hit tested first, because it is earlier in hit-test order. A FloatingActionButton over a native map works as you expect. A transparent Flutter widget with a GestureDetector over the native view blocks touches from reaching it, unless you set behavior: HitTestBehavior.translucent. Flutter's IgnorePointer and AbsorbPointer widgets work on the Flutter side, so you can use them to make an overlay pass touches through or swallow them deliberately.
Android specifics
The classic AndroidView takes the gestureRecognizers set described above. If you use the lower-level PlatformViewLink from the earlier composition-modes chapter, the same parameter sits on AndroidViewSurface, as in the example you saw there. Passing const <Factory<OneSequenceGestureRecognizer>>{} there means no recognizer competes on behalf of the native view. Add EagerGestureRecognizer to the set to claim touches.
Flutter converts its pointer events into Android MotionEvent objects and sends them to the native view. Your Android view then handles them with its usual onTouchEvent and gesture detectors. Native-side behavior such as requestDisallowInterceptTouchEvent has no effect on Flutter's arena, because the arena is decided before the events reach the native view.
iOS specifics
UiKitView takes the same gestureRecognizers set. The iOS embedder adds a layer of its own. It places a gesture recognizer over the native view that forwards touches to Flutter. Flutter then decides whether the native view's own recognizers should be allowed to continue. By default the native view's recognizers are blocked eagerly when Flutter's recognizers claim the touch.
When you register a view factory, there is a registrar variant that takes a gestureRecognizersBlockingPolicy. The default is eager. The other value, waitUntilTouchesEnded, delays blocking until the touch sequence finishes. That option can help when a native view uses its own gesture recognizers, such as a long press or a pinch, that clash with Flutter's decision. Check the API docs for your Flutter version before relying on it, because it is an embedder-level setting and not a Dart one.
Common errors
- The native map will not pan inside a
ListView. The scrollable won the vertical drag. AddEagerGestureRecognizer, or aVerticalDragGestureRecognizerand aHorizontalDragGestureRecognizerif you still want taps to be shared. - The list no longer scrolls.
EagerGestureRecognizercovers the full height of the list item. Make the native view smaller, or leave some Flutter padding beside it so the user can start a drag outside it. - Pinch zoom does nothing. A single-finger drag recognizer does not claim a multi-touch gesture. Add
ScaleGestureRecognizer, or use the eager recognizer. - A Flutter button over the native view does not respond. Check the widget order in the
Stack. The button must be after the native view. Also confirm that noAbsorbPointeris above it. - A tap goes to both layers. This is what
translucentdoes. Switch toopaque. - Gestures behave differently in a widget test. The test environment has no native view, so you cannot verify touch ownership there. Test on a device or an emulator.
Decision criteria
- Start with the empty set for static native content, because it leaves scrolling in Flutter's hands.
- Choose
EagerGestureRecognizerwhen the native view handles any free-form gesture and the user does not need to scroll the parent from inside it. - Choose specific drag or scale recognizers when you want the native view to take only some gestures and let the rest go to Flutter.
- Keep
hitTestBehavioratopaqueunless the native view is a backdrop for Flutter controls.
Exercise
A Flutter screen shows a native map in a 300-pixel-high box inside a vertical ListView. Users report that they can scroll the list by dragging on the map, but they cannot pan the map. What change fixes this, and what trade-off does it add?
Show answer
Pass a gestureRecognizers set containing Factory<OneSequenceGestureRecognizer>(() => EagerGestureRecognizer()) to AndroidView or UiKitView. The native view then wins drags that start on it, so the map pans. The trade-off is that a user can no longer scroll the list by dragging on the map. They must drag on the area outside it, so leave some Flutter content beside or around the map.
Exercise
You place a native video view under a Flutter play button in a Stack, and you want the view to ignore touches so Flutter widgets behind and around it receive them. Which PlatformViewHitTestBehavior do you pass, and why is the answer not translucent?
Show answer
Pass PlatformViewHitTestBehavior.transparent. With transparent, the native view does not receive touches, and widgets behind it do. With translucent, the native view would still receive each touch in addition to the widgets behind it, so it would react to taps you wanted it to ignore.