value<T extends Object> method

T value<T extends Object>({
  1. T xxLarge()?,
  2. T xLarge()?,
  3. T large()?,
  4. T medium()?,
  5. T small()?,
  6. T xSmall()?,
})

Builds the most suitable T depending on the current breakpoint.

Throws an ArgumentError if no argument is provided.

Because all of the builders are optional, the current breakpoint may not be supplied, meaning that we will always default to the smallest builder available in such cases.

Example:

The current breakpoint value is GranularBreakpoint.medium and we make the following call:

final granularLayout = context.granularLayout; // Get this context from somewhere
final text = value(
  xxLarge: () => 'Text for xxlarge!',
  xLarge: () => 'Text for xLarge!',
  large: () => 'Text for large or smaller',
);
final responsiveText = Text();

We will first check, respectively, if there is a medium, small, then a xSmall provided. In this case, because none is available, we will have to get the smallest available from the remaining, in this case, returning the larges builder value, the smallest of the available supplied builders.

Implementation

T value<T extends Object>({
  T Function()? xxLarge,
  T Function()? xLarge,
  T Function()? large,
  T Function()? medium,
  T Function()? small,
  T Function()? xSmall,
}) {
  if (xxLarge == null && xLarge == null && large == null && medium == null && small == null && xSmall == null) {
    throw ArgumentError('At least one breakpoint (xxLarge, xLarge, large, medium, small or xSmall) must be provided');
  }

  return closestValue({
    if (xxLarge != null) GranularBreakpoint.xxLarge: xxLarge(),
    if (xLarge != null) GranularBreakpoint.xLarge: xLarge(),
    if (large != null) GranularBreakpoint.large: large(),
    if (medium != null) GranularBreakpoint.medium: medium(),
    if (small != null) GranularBreakpoint.small: small(),
    if (xSmall != null) GranularBreakpoint.xSmall: xSmall(),
  });
}