TInputFieldTheme.defaultTheme constructor

TInputFieldTheme.defaultTheme(
  1. ColorScheme colors, {
  2. TInputSize size = defaultInputSize,
  3. TInputDecorationType decorationType = defaultInputDecorationType,
  4. TLabelPosition labelPosition = defaultLabelPosition,
})

Implementation

factory TInputFieldTheme.defaultTheme(
  ColorScheme colors, {
  TInputSize size = defaultInputSize,
  TInputDecorationType decorationType = defaultInputDecorationType,
  TLabelPosition labelPosition = defaultLabelPosition,
}) {
  final color = WidgetStateProperty.resolveWith((states) {
    if (states.contains(WidgetState.error)) return colors.error;
    if (states.contains(WidgetState.focused)) return colors.primary;
    if (states.contains(WidgetState.disabled)) return colors.outlineVariant;
    return colors.outline;
  });

  final backgroundColor = WidgetStateProperty.resolveWith((states) {
    if (decorationType == TInputDecorationType.filled) {
      return states.contains(WidgetState.disabled)
          ? colors.surface
          : states.contains(WidgetState.error)
              ? colors.errorContainer
              : colors.surfaceContainerLowest;
    }
    return states.contains(WidgetState.disabled) ? colors.surfaceContainerLowest : colors.surface;
  });

  final borderColor = WidgetStateProperty.resolveWith((states) {
    if (states.contains(WidgetState.error)) return colors.error;
    if (states.contains(WidgetState.focused)) return colors.primary;
    if (states.contains(WidgetState.disabled)) return colors.outlineVariant;
    return colors.outline;
  });

  final labelStyle = WidgetStateProperty.resolveWith((states) {
    return TextStyle(
      fontSize: labelPosition == TLabelPosition.aboveField ? 12.0 : 14.0,
      fontWeight: FontWeight.w500,
      color: states.contains(WidgetState.disabled)
          ? colors.onSurfaceVariant
          : states.contains(WidgetState.error)
              ? colors.error
              : colors.onSurfaceVariant,
    );
  });

  final helperTextStyle =
      WidgetStateProperty.all(TextStyle(fontSize: 12.0, fontWeight: FontWeight.w300, color: colors.onSurfaceVariant.withAlpha(200)));

  final errorTextStyle = WidgetStateProperty.all(TextStyle(fontSize: 12.0, fontWeight: FontWeight.w300, color: colors.error));

  final borderWidth = WidgetStateProperty.resolveWith((states) => states.contains(WidgetState.focused) ? 2.0 : 1.0);

  final borderRadius = WidgetStateProperty.resolveWith((states) {
    return switch (decorationType) {
      TInputDecorationType.outline => 8.0,
      TInputDecorationType.filled => 8.0,
      TInputDecorationType.underline => 0.0,
      TInputDecorationType.none => 0.0,
    };
  });

  return TInputFieldTheme(
    size: size,
    decorationType: decorationType,
    labelPosition: labelPosition,
    borderRadius: borderRadius,
    borderWidth: borderWidth,
    color: color,
    backgroundColor: backgroundColor,
    borderColor: borderColor,
    labelStyle: labelStyle,
    helperTextStyle: helperTextStyle,
    errorTextStyle: errorTextStyle,
    tagStyle: labelStyle,
    hintStyle: WidgetStateProperty.all(TextStyle(color: colors.onSurfaceVariant.withAlpha(150))),
    labelBuilder: _buildLabelBuilder(labelStyle, labelStyle, errorTextStyle, backgroundColor),
    helperTextBuilder: _buildHelperTextBuilder(helperTextStyle, size.padding),
    errorsBuilder: _buildErrorsBuilder(errorTextStyle, size.padding),
  );
}