semantics property
final
Describes the semantics notations that should be added to the underlying render object RenderSemanticsGestureHandler.
It has no effect if excludeFromSemantics is true.
When semantics is null, RawGestureDetector will fall back to a default delegate which checks if the detector owns certain gesture recognizers and calls their callbacks if they exist:
- During a semantic tap, it calls TapGestureRecognizer's
onTapDown
,onTapUp
, andonTap
. - During a semantic long press, it calls LongPressGestureRecognizer's
onLongPressDown
,onLongPressStart
,onLongPress
,onLongPressEnd
andonLongPressUp
. - During a semantic horizontal drag, it calls HorizontalDragGestureRecognizer's
onDown
,onStart
,onUpdate
andonEnd
, then PanGestureRecognizer'sonDown
,onStart
,onUpdate
andonEnd
. - During a semantic vertical drag, it calls VerticalDragGestureRecognizer's
onDown
,onStart
,onUpdate
andonEnd
, then PanGestureRecognizer'sonDown
,onStart
,onUpdate
andonEnd
.
This custom gesture detector listens to force presses, while also allows
the same callback to be triggered by semantic long presses.
link
class ForcePressGestureDetectorWithSemantics extends StatelessWidget {
const ForcePressGestureDetectorWithSemantics({
super.key,
required this.child,
required this.onForcePress,
});
final Widget child;
final VoidCallback onForcePress;
@override
Widget build(BuildContext context) {
return RawGestureDetector(
gestures: <Type, GestureRecognizerFactory>{
ForcePressGestureRecognizer: GestureRecognizerFactoryWithHandlers<ForcePressGestureRecognizer>(
() => ForcePressGestureRecognizer(debugOwner: this),
(ForcePressGestureRecognizer instance) {
instance.onStart = (_) => onForcePress();
}
),
},
behavior: HitTestBehavior.opaque,
semantics: _LongPressSemanticsDelegate(onForcePress),
child: child,
);
}
}
class _LongPressSemanticsDelegate extends SemanticsGestureDelegate {
_LongPressSemanticsDelegate(this.onLongPress);
VoidCallback onLongPress;
@override
void assignSemantics(RenderSemanticsGestureHandler renderObject) {
renderObject.onLongPress = onLongPress;
}
}
Implementation
final SemanticsGestureDelegate? semantics;