relocate method

void relocate()

Relocates the floating panel accounting the possible intersections.

Implementation

void relocate() {
  if (dragged.isFalse && scaled.isFalse && !_floatingRelocated) {
    _floatingRelocated = true;

    final Rect? floatingBounds = floatingKey.globalPaintBounds;
    Rect intersect =
        floatingBounds?.intersect(intersection?.value ?? Rect.zero) ??
        Rect.zero;

    intersect = Rect.fromLTWH(
      intersect.left,
      intersect.top,
      intersect.width,
      intersect.height == 0 ? 0 : intersect.height + 10,
    );

    if (intersect.width > 0 && intersect.height > 0) {
      // Intersection is non-zero, so move the floating panel up.
      if (bottom.value != null) {
        bottom.value = bottom.value! + intersect.height;
      } else {
        top.value = top.value! - intersect.height;
      }

      applyConstraints();
    } else if ((intersect.height < 0 || intersect.width < 0) &&
        bottomShifted != null) {
      // Intersection is less than zero and the floating panel is higher than
      // it was before, so move it to its original position.
      double bottom =
          this.bottom.value ?? size.height - top.value! - height.value;
      if (bottom > bottomShifted!) {
        double difference = bottom - bottomShifted!;
        if (this.bottom.value != null) {
          if (difference.abs() < intersect.height.abs() ||
              intersect.width < 0) {
            this.bottom.value = bottomShifted;
          } else {
            this.bottom.value = this.bottom.value! + intersect.height;
          }
        } else {
          if (difference.abs() < intersect.height.abs() ||
              intersect.width < 0) {
            top.value = size.height - height.value - bottomShifted!;
          } else {
            top.value = top.value! - intersect.height;
          }
        }

        applyConstraints();
      }
    }

    SchedulerBinding.instance.addPostFrameCallback(
      (_) => _floatingRelocated = false,
    );
  }
}