loadLabel function

Widget loadLabel({
  1. required BuildContext context,
  2. String? label,
  3. bool isRequired = false,
})

Builds the label row above an input using HostConfig label styles.

Implementation

Widget loadLabel({
  required BuildContext context,
  String? label,
  bool isRequired = false,
}) {
  if (label == null) {
    return const SizedBox();
  }

  final resolver = ProviderScope.containerOf(
    context,
  ).read(styleReferenceResolverProvider);
  final inputsConfig = resolver.getInputsConfig();
  final labelConfig = isRequired
      ? inputsConfig?.label.requiredInputs
      : inputsConfig?.label.optionalInputs;

  final color = resolver.resolveContainerForegroundColor(
    style: labelConfig?.color ?? 'default',
    isSubtle: labelConfig?.isSubtle ?? false,
  );
  final double fontSize = resolver.resolveFontSize(
    context: context,
    sizeString: labelConfig?.size ?? 'default',
  );
  final FontWeight fontWeight = resolver.resolveFontWeight(
    labelConfig?.weight ?? 'default',
  );

  final double bottomPadding = resolver.resolveSpacing(
    inputsConfig?.label.inputSpacing,
  );

  return Align(
    alignment: Alignment.centerLeft,
    child: Padding(
      padding: EdgeInsets.only(bottom: bottomPadding, top: 0),
      child: Text.rich(
        TextSpan(
          children: [
            TextSpan(
              text: label,
              style: TextStyle(
                color: color,
                fontSize: fontSize,
                fontWeight: fontWeight,
              ),
            ),
            if (labelConfig?.suffix.isNotEmpty ?? false)
              TextSpan(
                text: labelConfig?.suffix,
                style: TextStyle(
                  color: color,
                  fontSize: fontSize,
                  fontWeight: fontWeight,
                ),
              ),
            if (isRequired)
              TextSpan(
                text: ' *',
                style: TextStyle(
                  color:
                      resolver.resolveContainerForegroundColor(
                        style: 'attention',
                        isSubtle: false,
                      ) ??
                      Theme.of(context).colorScheme.error,
                ),
              ),
          ],
        ),
      ),
    ),
  );
}