Feedback class abstract final

Provides platform-specific acoustic and/or haptic feedback for certain actions.

For example, to play the Android-typically click sound when a button is tapped, call forTap. For the Android-specific vibration when long pressing an element, call forLongPress. Alternatively, you can also wrap your GestureDetector.onTap or GestureDetector.onLongPress callback in wrapForTap or wrapForLongPress to achieve the same (see example code below).

Calling any of these methods is a no-op on iOS as actions on that platform typically don't provide haptic or acoustic feedback.

All methods in this class are usually called from within a StatelessWidget.build method or from a State's methods as you have to provide a BuildContext.

To trigger platform-specific feedback before executing the actual callback:
link
class WidgetWithWrappedHandler extends StatelessWidget {
  const WidgetWithWrappedHandler({super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: Feedback.wrapForTap(_onTapHandler, context),
      onLongPress: Feedback.wrapForLongPress(_onLongPressHandler, context),
      child: const Text('X'),
    );
  }

  void _onTapHandler() {
    // Respond to tap.
  }

  void _onLongPressHandler() {
    // Respond to long press.
  }
}

Alternatively, you can also call forTap or forLongPress directly within your tap or long press handler:
link
class WidgetWithExplicitCall extends StatelessWidget {
  const WidgetWithExplicitCall({super.key});

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        // Do some work (e.g. check if the tap is valid)
        Feedback.forTap(context);
        // Do more work (e.g. respond to the tap)
      },
      onLongPress: () {
        // Do some work (e.g. check if the long press is valid)
        Feedback.forLongPress(context);
        // Do more work (e.g. respond to the long press)
      },
      child: const Text('X'),
    );
  }
}

Constructors

Feedback()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

forLongPress(BuildContext context) Future<void>
Provides platform-specific feedback for a long press.
forTap(BuildContext context) Future<void>
Provides platform-specific feedback for a tap.
wrapForLongPress(GestureLongPressCallback? callback, BuildContext context) GestureLongPressCallback?
Wraps a GestureLongPressCallback to provide platform specific feedback for a long press before the provided callback is executed.
wrapForTap(GestureTapCallback? callback, BuildContext context) GestureTapCallback?
Wraps a GestureTapCallback to provide platform specific feedback for a tap before the provided callback is executed.