loadErrorMessage function

Widget loadErrorMessage({
  1. required BuildContext context,
  2. String? errorMessage,
  3. bool showError = false,
})

Builds the validation error text below an input when showError is true.

Implementation

Widget loadErrorMessage({
  required BuildContext context,
  String? errorMessage,
  bool showError = false,
}) {
  if (errorMessage == null || !showError) {
    return const SizedBox();
  }

  final resolver = ProviderScope.containerOf(
    context,
  ).read(styleReferenceResolverProvider);
  final inputsConfig = resolver.getInputsConfig();
  final errorMessageConfig = inputsConfig?.errorMessage;

  final color = resolver.resolveContainerForegroundColor(
    style: 'attention', // Error messages usually use attention colo
    isSubtle: false,
  );
  final double fontSize = resolver.resolveFontSize(
    context: context,
    sizeString: errorMessageConfig?.size,
  );
  final FontWeight fontWeight = resolver.resolveFontWeight(
    errorMessageConfig?.weight ?? 'default',
  );
  final double topPadding = resolver.resolveSpacing(
    errorMessageConfig?.spacing,
  );

  return Align(
    alignment: Alignment.centerLeft,
    child: Padding(
      padding: EdgeInsets.only(top: topPadding, bottom: 0),
      child: Text(
        errorMessage,
        style: TextStyle(
          color: color,
          fontSize: fontSize,
          fontWeight: fontWeight,
        ),
      ),
    ),
  );
}