coerced method
Provides a ViewPort which ViewPort.width and ViewPort.height values
will be lower bounded by the provided minWidth
and minHeight
and
upper bounded by maxWidth
and maxHeight
.
Simply said, if the decorated
values are lesser than the defined minimal
bounds, then the minimal bounds' values are returned (e.g.
decorated.width = 500 and minWidth
= 600, then 600 will be returned),
and if the decorated
values are bigger than the defined maximal bounds,
then the maximal bounds' are returned (e.g. decorated.width = 1500 and
maxWidth
= 1024, then 1024 will be returned).
minWidth
describes the width's lower bound, minHeight
- height's lower
bound. Minimal bounds are 0 by default. maxWidth
describes the width's
upper bound, maxHeight
- height's upper bound. Maximal bounds are set to
double.infinity by default.
Invocation of this method with all the parameters provided with the
default values will produce no additional effect over the direct
decorated
instance usage unless there are specific cases like
decorated.width resulting into double.infinity, for which the result is
unspecified.
Throws AssertionError if minHeight
> maxHeight
or
minWidth
> maxWidth
final ViewPort vp = const ViewPorts().coerced(
const ViewPorts().fromMediaQueryData(MediaQuery.of(context)),
minWidth: 200.0,
maxWidth: 1024.0,
);
Implementation
ViewPort coerced(
ViewPort decorated, {
double minHeight = 0,
double minWidth = 0,
double maxHeight = double.infinity,
double maxWidth = double.infinity,
}) {
assert(minHeight <= maxHeight, 'minHeight > maxHeight, which is forbidden');
assert(minWidth <= maxWidth, 'minWidth > maxWidth, which is forbidden');
return minOf(
FixedViewPort(height: maxHeight, width: maxWidth),
maxOf(
FixedViewPort(height: minHeight, width: minWidth),
decorated,
));
}