getConstraint function

Constraint getConstraint(
  1. Widget widget,
  2. LayoutDirection direction, {
  3. int crossSize = 0,
  4. Element? element,
})

Undocumented public member.

Retrieves the layout constraint for the given widget. If the optional element parameter is provided, we query its intrinsic size by calling the element's polymorphic layout getters.

Implementation

Constraint getConstraint(
  Widget widget,
  LayoutDirection direction, {
  int crossSize = 0,
  Element? element,
}) {
  if (widget is Flexible) {
    return FlexConstraint(widget.flex);
  }
  if (widget is SizedBox) {
    final size = direction == LayoutDirection.horizontal
        ? widget.width
        : widget.height;
    if (size != null) {
      return LengthConstraint(size);
    }
  }
  if (direction == LayoutDirection.vertical) {
    final intH = element != null
        ? element.getIntrinsicHeight(crossSize)
        : widget.getIntrinsicHeight(crossSize);
    if (intH > 0) {
      return LengthConstraint(intH);
    }
  } else {
    final intW = element != null
        ? element.getIntrinsicWidth(crossSize)
        : widget.getIntrinsicWidth(crossSize);
    if (intW > 0) {
      return LengthConstraint(intW);
    }
  }
  return const FlexConstraint(1);
}