value<T extends Object> method

T value<T extends Object>({
  1. T desktop()?,
  2. T tablet()?,
  3. T phone()?,
  4. T tinyHardware()?,
})

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 CommonBreakpoint.phone and we make the following call:

final commonLayout = context.commonLayout; // Get this context from somewhere
final responsiveText = Text(value(desktop: () => 'Desktop text', tablet: () => 'Mobile text'));

We will first check if there is a phone, then a tinyHardware provided. In this case, because both aren't available, we will have to get the smallest available from the remaining, in this case, returning the tablets builder value.

Implementation

T value<T extends Object>({
  T Function()? desktop,
  T Function()? tablet,
  T Function()? phone,
  T Function()? tinyHardware,
}) {
  if (desktop == null && tablet == null && phone == null && tinyHardware == null) {
    throw ArgumentError('At least one breakpoint (desktop, tablet, phone or tinyHardware) must be provided');
  }

  return closestValue({
    if (desktop != null) CommonBreakpoint.desktop: desktop(),
    if (tablet != null) CommonBreakpoint.tablet: tablet(),
    if (phone != null) CommonBreakpoint.phone: phone(),
    if (tinyHardware != null) CommonBreakpoint.tinyHardware: tinyHardware(),
  });
}