constrainedWidth function

double constrainedWidth(
  1. BuildContext context,
  2. double preferredWidth, {
  3. double? constraintWidth,
  4. bool? shouldConstraint,
})

Method to get preferredWidth constrained, if is bigger than media query width, will return constraintWidth if not null, else return media query.

Implementation

double constrainedWidth (BuildContext context, double preferredWidth, {double? constraintWidth, bool? shouldConstraint}) {
  double width = preferredWidth;
  double def = constraintWidth ?? MediaQuery.of(context).size.width;
  bool constraint = shouldConstraint  ?? preferredWidth > def;

  if (constraint) {
    width = def;
  }

  return width;
}