within<T> function

Matcher within<T>({
  1. required num distance,
  2. required T from,
  3. DistanceFunction<T>? distanceFunction,
})

Asserts that two values are within a certain distance from each other.

The distance is computed by a DistanceFunction.

If distanceFunction is null, a standard distance function is used for the T generic argument. Standard functions are defined for the following types:

  • Color, whose distance is the maximum component-wise delta.
  • Offset, whose distance is the Euclidean distance computed using the method Offset.distance.
  • Rect, whose distance is the maximum component-wise delta.
  • Size, whose distance is the Offset.distance of the offset computed as the difference between two sizes.
  • int, whose distance is the absolute difference between two integers.
  • double, whose distance is the absolute difference between two doubles.

See also:

  • moreOrLessEquals, which is similar to this function, but specializes in doubles and has an optional epsilon parameter.
  • rectMoreOrLessEquals, which is similar to this function, but specializes in Rects and has an optional epsilon parameter.
  • closeTo, which specializes in numbers only.

Implementation

Matcher within<T>({
  required num distance,
  required T from,
  DistanceFunction<T>? distanceFunction,
}) {
  distanceFunction ??= _kStandardDistanceFunctions[T] as DistanceFunction<T>?;

  if (distanceFunction == null) {
    throw ArgumentError(
      'The specified distanceFunction was null, and a standard distance '
      'function was not found for type ${from.runtimeType} of the provided '
      '`from` argument.'
    );
  }

  return _IsWithinDistance<T>(distanceFunction, from, distance);
}