calculateTranslationOffset function

Offset calculateTranslationOffset({
  1. required Alignment alignment,
})

Calculates the translational offset that the dialog should take relative to the BuildContext of the widget that opened it. For example, if the specified alignment is Alignment.bottomRight, then since positional offset calculation positions the dialog at the bottom right of the widget that opened it, the translational offset will need to apply an x-axis translation of Offset(-1.0, 0) since the dialog's coordinates start top-left and to have it's right bound end at the right bound of the widget that opened it, it needs a negative translation equal to its own width.

Implementation

Offset calculateTranslationOffset({
  required Alignment alignment,
}) {
  if (alignment == Alignment.bottomLeft) {
    return const Offset(0, -1.0);
  } else if (alignment == Alignment.bottomCenter) {
    return const Offset(-0.5, -1.0);
  } else if (alignment == Alignment.bottomRight) {
    return const Offset(-1.0, -1.0);
  } else if (alignment == Alignment.centerLeft) {
    return const Offset(0, -0.5);
  } else if (alignment == Alignment.center) {
    return const Offset(-0.5, -0.5);
  } else if (alignment == Alignment.centerRight) {
    return const Offset(-1.0, -0.5);
  } else if (alignment == Alignment.topLeft) {
    return Offset.zero;
  } else if (alignment == Alignment.topCenter) {
    return const Offset(-0.5, 0);
  } else if (alignment == Alignment.topRight) {
    return const Offset(-1.0, 0);
  }

  throw ('Unsupported alignment');
}