updateSecondaryAttach method

void updateSecondaryAttach()

Calculates the appropriate secondaryLeft, secondaryRight, secondaryTop and secondaryBottom values according to the nearest edge.

Implementation

void updateSecondaryAttach() {
  secondaryLeft.value ??=
      size.width - secondaryWidth.value - (secondaryRight.value ?? 0);
  secondaryTop.value ??=
      size.height - secondaryHeight.value - (secondaryBottom.value ?? 0);

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

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

  secondaryTop.value = null;
  secondaryLeft.value = null;
  secondaryRight.value = null;
  secondaryBottom.value = null;

  if (align == Alignment.topLeft) {
    secondaryTop.value = top;
    secondaryLeft.value = left;
  } else if (align == Alignment.topRight) {
    secondaryTop.value = top;
    secondaryRight.value =
        secondaryWidth.value + left <= size.width
            ? secondaryRight.value = size.width - left - secondaryWidth.value
            : 0;
  } else if (align == Alignment.bottomLeft) {
    secondaryLeft.value = left;
    secondaryBottom.value =
        top + secondaryHeight.value <= size.height
            ? size.height - top - secondaryHeight.value
            : 0;
  } else if (align == Alignment.bottomRight) {
    secondaryRight.value =
        secondaryWidth.value + left <= size.width
            ? size.width - left - secondaryWidth.value
            : 0;
    secondaryBottom.value =
        top + secondaryHeight.value <= size.height
            ? size.height - top - secondaryHeight.value
            : 0;
  }

  relocateSecondary();
}