resolveWith method

PaywallStyle resolveWith({
  1. bool? selected,
  2. PaywallScaler? scaler,
  3. TextDirection? textDirection,
})

Implementation

PaywallStyle resolveWith({
  bool? selected,
  PaywallScaler? scaler,
  TextDirection? textDirection,
}) {
  double? dp(double? value) {
    if (value == null || value == 0 || value.isInfinite) return value;
    if (scaler == null) return value;
    return scaler(value);
  }

  T td<T>(T a, T b) {
    if (textDirection == null) return a;
    return textDirection == TextDirection.rtl ? b : a;
  }

  Offset? resolveOffset(Offset? e) {
    if (e == null) return null;
    return Offset(dp(e.dx) ?? 0, dp(e.dy) ?? 0);
  }

  Alignment? resolveAlignment(Alignment? e) {
    if (e == null || textDirection == null) return e;
    if (textDirection == TextDirection.ltr) return e;
    return Alignment(-e.x, e.y);
  }

  EdgeInsets? resolveEdge(EdgeInsets? e) {
    if (e == null) return null;
    return e.copyWith(
      left: dp(td(e.left, e.right)),
      right: dp(td(e.right, e.left)),
      top: dp(e.top),
      bottom: dp(e.bottom),
    );
  }

  return copyWith(
    selected: selected,
    alignmentState: alignmentState?.resolveWith(resolveAlignment),
    contentAlignmentState:
        contentAlignmentState?.resolveWith(resolveAlignment),
    widthState: widthState?.resolveWith(dp),
    heightState: heightState?.resolveWith(dp),
    blurState: blurState?.resolveWith(dp),
    borderState: borderState?.resolveWith((e) {
      if (e == null) return null;
      BorderSide resolve(BorderSide s) => s.copyWith(width: dp(s.width));
      return Border(
        top: resolve(e.top),
        bottom: resolve(e.bottom),
        left: resolve(td(e.left, e.right)),
        right: resolve(td(e.right, e.left)),
      );
    }),
    borderRadiusState: borderRadiusState?.resolveWith((value) {
      if (value == null) return null;
      Radius resolve(Radius e) {
        return Radius.elliptical(dp(e.x) ?? 0, dp(e.y) ?? 0);
      }

      return value.copyWith(
        topLeft: resolve(td(value.topLeft, value.topRight)),
        topRight: resolve(td(value.topRight, value.topLeft)),
        bottomRight: resolve(td(value.bottomRight, value.bottomLeft)),
        bottomLeft: resolve(td(value.bottomLeft, value.bottomRight)),
      );
    }),
    boxShadowState: boxShadowState?.resolveWith((values) {
      return values?.map((value) {
        return value.copyWith(
          offset: resolveOffset(value.offset),
          blurRadius: dp(value.blurRadius),
          spreadRadius: dp(value.spreadRadius),
        );
      }).toList();
    }),
    constraintsState: constraintsState?.resolveWith((e) {
      if (e == null) return null;
      return e.copyWith(
        minWidth: dp(e.minWidth),
        maxWidth: dp(e.maxWidth),
        minHeight: dp(e.minHeight),
        maxHeight: dp(e.maxHeight),
      );
    }),
    marginState: marginState?.resolveWith(resolveEdge),
    paddingState: paddingState?.resolveWith(resolveEdge),
    positionState: positionState?.resolveWith((value) {
      return value?.resolveWith(
        scaler: scaler,
        textDirection: textDirection,
      );
    }),
    foregroundGradientState: foregroundGradientState?.resolveWith((e) {
      if (e == null || e.colors.length < 2) return null;
      final begin = e.begin;
      final end = e.end;
      return LinearGradient(
        begin: begin is Alignment ? resolveAlignment(begin) ?? begin : begin,
        end: end is Alignment ? resolveAlignment(end) ?? end : end,
        colors: e.colors,
        stops: e.stops,
        tileMode: e.tileMode,
        transform: e.transform,
      );
    }),
    gradientState: gradientState?.resolveWith((e) {
      if (e == null || e.colors.length < 2) return null;
      final begin = e.begin;
      final end = e.end;
      return LinearGradient(
        begin: begin is Alignment ? resolveAlignment(begin) ?? begin : begin,
        end: end is Alignment ? resolveAlignment(end) ?? end : end,
        colors: e.colors,
        stops: e.stops,
        tileMode: e.tileMode,
        transform: e.transform,
      );
    }),
    sizeState: sizeState?.resolveWith(dp),
    textStyleState: textStyleState?.resolveWith((value) {
      return value?.copyWith(
        fontSize: dp(value.fontSize),
        decorationThickness: dp(value.decorationThickness),
      );
    }),
    layoutAlignmentState: layoutAlignmentState?.resolveWith(resolveAlignment),
    layoutCrossAxisAlignmentState:
        layoutCrossAxisAlignmentState?.resolveWith((e) {
      if (e == null) return null;
      switch (e) {
        case CrossAxisAlignment.start:
          return td(CrossAxisAlignment.start, CrossAxisAlignment.end);
        case CrossAxisAlignment.end:
          return td(CrossAxisAlignment.end, CrossAxisAlignment.start);
        default:
          return e;
      }
    }),
    layoutRunSpacingState: layoutRunSpacingState?.resolveWith(dp),
    layoutSpacingState: layoutSpacingState?.resolveWith(dp),
    layoutWrapCrossAlignmentState:
        layoutWrapCrossAlignmentState?.resolveWith((e) {
      if (e == null) return null;
      switch (e) {
        case WrapCrossAlignment.start:
          return td(WrapCrossAlignment.start, WrapCrossAlignment.end);
        case WrapCrossAlignment.end:
          return td(WrapCrossAlignment.end, WrapCrossAlignment.start);
        default:
          return e;
      }
    }),
  );
}