TouchRippleState constructor

TouchRippleState({
  1. required TickerProvider vsync,
  2. required TouchRippleBehavior behavior,
  3. required VoidCallback callback,
  4. required void onDismissed(
    1. TouchRippleState state
    ),
  5. required Offset eventedOffset,
  6. bool isRejectable = false,
})

Initializes several animation instance needed to implement the touch ripple effect.

Initializes the spread animation and defines the fade behavior based on the state of the spread animation,

If the spread animation value has updated, it will continue to check the animation value until it can call the event callback function,

When create a TouchRippleState instance while the gesture is not accepted, the event callback function call postponed until the gesture is accepted.

  • When we say that a gesture is not accepted, we are primarily referring to situations where the gesture may be rejected midway through, or where we create a touch ripple state instance with the gesture not accepted in order to show the user the effect in advance.

Initializes the fade animation and defines the behavior based on the state of the fade animation. For example, if the fade-out state is complete, call the given onDismissed so that this instance can be dispatched from the controller.

Arguments:

  • The given callback is invoked at the the event callback timing corresponding to the given behavior.

  • The given isRejectable defines whether the instance was initialized while the gesture was not yet accepted.

See also:

  • Call a given callback function based on the state of the animation.

Implementation

TouchRippleState({
  required TickerProvider vsync,
  required TouchRippleBehavior behavior,
  required VoidCallback callback,
  required void Function(TouchRippleState state) onDismissed,
  required this.eventedOffset,
  bool isRejectable = false,
}) : _isRejectable = isRejectable,
     _behavior = behavior
{
  assert(behavior.spreadDuration != null, 'Spread duration of touch ripple behavior was not initialized.');
  _spreadAnimation = AnimationController(
    vsync: vsync,
    duration: behavior.spreadDuration ?? Duration.zero,
  );

  // Call the event callback function when the current animation
  // progress can call the event callback function.
  void checkEventCallBack() {
    assert(behavior.lowerPercent != null, 'Lower percent of touch ripple behavior was not initialized.');
    final lowerPercent = behavior.lowerPercent ?? 0;

    if (_isRejectable) return;
    if (spreadPercent >= (behavior.eventCallBackableMinPercent ?? lowerPercent)) {
      callback.call();
      // Deregistering the listener as there is no longer a need to know
      // when to invoke the event callback function.
      _spreadAnimation.removeListener(checkEventCallBack);
    }
  }
  _spreadAnimation.addListener(checkEventCallBack);
  _spreadAnimation.addStatusListener((status) {
    if (status == AnimationStatus.forward) fadeIn();
    if (status == AnimationStatus.completed && _isRejectable == false) fadeOut();
  });

  assert(behavior.spreadCurve != null, 'Spread curve of touch ripple behavior was not initialized.');
  _spreadCurved = CurvedAnimation(
    parent: _spreadAnimation,
    curve: behavior.spreadCurve ?? Curves.linear,
  );

  _fadeAnimation = AnimationController(
    vsync: vsync,
    duration: behavior.fadeInDuration,
    reverseDuration: behavior.fadeOutDuration,
  );
  _fadeAnimation.addStatusListener((status) {
    if (status == AnimationStatus.dismissed) onDismissed.call(this);
  });

  assert(behavior.fadeInCurve != null, 'Fade in curve of touch ripple behavior was not initialized.');
  assert(behavior.fadeOutCurve != null, 'Fade out curve of touch ripple behavior was not initialized.');
  _fadeCurved = CurvedAnimation(
    parent: _fadeAnimation,
    curve: behavior.fadeInCurve ?? Curves.linear,
    reverseCurve: behavior.fadeOutCurve,
  );
}