applyConstraints method

Widget applyConstraints(
  1. Widget view,
  2. Constraints constraints
)

This routine applies the given constraints to the supplied view and then returns a widget with the view wrapped in those constraints

Implementation

Widget applyConstraints(Widget view, Constraints constraints) {
  // If no constraints are specified
  // return the view
  if (constraints.isEmpty) return view;

  // if parent is a Box Model these constraints
  // have already been applied
  if (this.model?.parent is BoxModel) return view;

  // Apply min and max constraints to the view only if
  // they are supplied and have not already been applied using
  // width and/or height
  if ((constraints.minWidth ?? 0) > 0 ||
      (constraints.maxWidth ?? double.infinity) < double.infinity ||
      (constraints.minHeight ?? 0) > 0 ||
      (constraints.maxHeight ?? double.infinity) < double.infinity) {
    var box = BoxConstraints(
        minWidth: constraints.minWidth ?? 0,
        maxWidth: constraints.maxWidth ?? double.infinity,
        minHeight: constraints.minHeight ?? 0,
        maxHeight: constraints.maxHeight ?? double.infinity);
    view = ConstrainedBox(constraints: box, child: view);
  }

  // If a width is specified
  // wrap the view in an unconstrained box of the specified
  // width and allow existing vertical constraints
  if (constraints.width != null && constraints.height == null) {
    view = UnconstrainedBox(
        constrainedAxis: Axis.vertical,
        child: SizedBox(width: constraints.width, child: view));
  }

  // If a height is specified
  // wrap the view in an unconstrained box of the specified
  // height and allow existing horizontal constraints
  else if (constraints.width == null && constraints.height != null) {
    view = UnconstrainedBox(
        constrainedAxis: Axis.horizontal,
        child: SizedBox(height: constraints.height, child: view));
  }

  // If both width and height are specified
  // wrap the view in an unconstrained box of the specified
  // width and height
  else if (constraints.width != null && constraints.height != null) {
    view = UnconstrainedBox(
        child: SizedBox(
            width: constraints.width,
            height: constraints.height,
            child: view));
  }

  return view;
}