getActiveCondition method Null safety

Condition? getActiveCondition(
  1. BuildContext context,
  2. List<Condition> conditions
)

Set activeCondition. The active condition is found by matching the search criteria in order of precedence:

  1. Conditional.EQUALS Named breakpoints from a parent ResponsiveWrapper.
  2. Conditional.SMALLER_THAN a. Named breakpoints. b. Unnamed breakpoints.
  3. Conditional.LARGER_THAN a. Named breakpoints. b. Unnamed breakpoints. Returns null if no Active Condition is found.

Implementation

Condition? getActiveCondition(
    BuildContext context, List<Condition> conditions) {
  Condition? equalsCondition = conditions.firstWhereOrNull((element) {
    if (element.condition == Conditional.EQUALS) {
      return ResponsiveWrapper.of(context).activeBreakpoint.name ==
          element.name;
    }

    return false;
  });
  if (equalsCondition != null) {
    return equalsCondition;
  }

  Condition? smallerThanCondition = conditions.firstWhereOrNull((element) {
    if (element.condition == Conditional.SMALLER_THAN) {
      if (element.name != null) {
        return ResponsiveWrapper.of(context).isSmallerThan(element.name!);
      }

      return MediaQuery.of(context).size.width < element.breakpoint!;
    }
    return false;
  });
  if (smallerThanCondition != null) {
    return smallerThanCondition;
  }

  Condition? largerThanCondition =
      conditions.reversed.firstWhereOrNull((element) {
    if (element.condition == Conditional.LARGER_THAN) {
      if (element.name != null) {
        return ResponsiveWrapper.of(context).isLargerThan(element.name);
      }

      return MediaQuery.of(context).size.width >= element.breakpoint!;
    }
    return false;
  });
  if (largerThanCondition != null) {
    return largerThanCondition;
  }

  return null;
}