semantics property Null safety

SemanticsGestureDelegate? semantics
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:

This custom gesture detector listens to force presses, while also allows the same callback to be triggered by semantic long presses.
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;