updateAttach method

void updateAttach()

Calculates the appropriate left, right, top and bottom values according to the nearest edge.

Implementation

void updateAttach() {
  this.left.value ??= size.width - width.value - (right.value ?? 0);
  this.top.value ??= size.height - height.value - (bottom.value ?? 0);

  List<MapEntry<Alignment, double>> alignments = [
    MapEntry(
      Alignment.topLeft,
      Point(
        this.left.value!,
        this.top.value!,
      ).squaredDistanceTo(const Point(0, 0)),
    ),
    MapEntry(
      Alignment.topRight,
      Point(
        this.left.value! + width.value,
        this.top.value!,
      ).squaredDistanceTo(Point(size.width, 0)),
    ),
    MapEntry(
      Alignment.bottomLeft,
      Point(
        this.left.value!,
        this.top.value! + height.value,
      ).squaredDistanceTo(Point(0, size.height)),
    ),
    MapEntry(
      Alignment.bottomRight,
      Point(
        this.left.value! + width.value,
        this.top.value! + height.value,
      ).squaredDistanceTo(Point(size.width, size.height)),
    ),
  ]..sort((e1, e2) => e1.value.compareTo(e2.value));

  Alignment align = alignments.first.key;
  double left = this.left.value!;
  double top = this.top.value!;

  this.top.value = null;
  this.left.value = null;
  right.value = null;
  bottom.value = null;

  if (align == Alignment.topLeft) {
    this.top.value = top;
    this.left.value = left;
  } else if (align == Alignment.topRight) {
    this.top.value = top;
    right.value =
        width.value + left <= size.width
            ? right.value = size.width - left - width.value
            : 0;
  } else if (align == Alignment.bottomLeft) {
    this.left.value = left;
    bottom.value =
        top + height.value <= size.height
            ? size.height - top - height.value
            : 0;
  } else if (align == Alignment.bottomRight) {
    right.value =
        width.value + left <= size.width
            ? size.width - left - width.value
            : 0;
    bottom.value =
        top + height.value <= size.height
            ? size.height - top - height.value
            : 0;
  }

  bottomShifted = bottom.value ?? size.height - top - height.value;
  relocate();
}