ZetaSearchBar constructor

ZetaSearchBar({
  1. AutovalidateMode? autovalidateMode,
  2. FormFieldValidator<String>? validator,
  3. FormFieldSetter<String>? onSaved,
  4. ValueChanged<String?>? onChange,
  5. @Deprecated('Use onFieldSubmitted instead. ' 'deprecated as of 0.15.0') ValueChanged<String?>? onSubmit,
  6. ValueChanged<String?>? onFieldSubmitted,
  7. ZetaFormFieldRequirement? requirementLevel,
  8. TextEditingController? controller,
  9. bool disabled = false,
  10. String? initialValue,
  11. ZetaWidgetSize size = ZetaWidgetSize.medium,
  12. ZetaWidgetBorder shape = ZetaWidgetBorder.rounded,
  13. @Deprecated('Use hintText instead. ' 'deprecated as of 0.15.0') String? hint,
  14. String? hintText,
  15. Future<String?> onSpeechToText()?,
  16. bool showSpeechToText = true,
  17. @Deprecated('Use disabled instead. ' 'enabled is deprecated as of 0.11.0') bool enabled = true,
  18. FocusNode? focusNode,
  19. TextInputAction? textInputAction,
  20. String? microphoneSemanticLabel,
  21. String? clearSemanticLabel,
  22. Key? key,
  23. @Deprecated('Show leading icon is deprecated as of 0.14.2') bool showLeadingIcon = true,
  24. @Deprecated('Use onChange instead') ValueChanged<String?>? onChanged,
})

Constructor for ZetaSearchBar.

Implementation

ZetaSearchBar({
  super.autovalidateMode,
  super.validator,
  super.onSaved,
  super.onChange,
  @Deprecated('Use onFieldSubmitted instead. ' 'deprecated as of 0.15.0') ValueChanged<String?>? onSubmit,
  super.onFieldSubmitted,
  super.requirementLevel,
  super.controller,
  super.disabled = false,
  super.initialValue,
  this.size = ZetaWidgetSize.medium,
  this.shape = ZetaWidgetBorder.rounded,
  @Deprecated('Use hintText instead. ' 'deprecated as of 0.15.0') String? hint,
  this.hintText,
  this.onSpeechToText,
  this.showSpeechToText = true,
  @Deprecated('Use disabled instead. ' 'enabled is deprecated as of 0.11.0') bool enabled = true,
  this.focusNode,
  this.textInputAction,
  this.microphoneSemanticLabel,
  this.clearSemanticLabel,
  super.key,
  @Deprecated('Show leading icon is deprecated as of 0.14.2') bool showLeadingIcon = true,
  @Deprecated('Use onChange instead') ValueChanged<String?>? onChanged,
}) : super(
        builder: (field) {
          final zeta = Zeta.of(field.context);

          final _ZetaSearchBarState state = field as _ZetaSearchBarState;

          final BorderRadius borderRadius = switch (shape) {
            ZetaWidgetBorder.rounded => zeta.radius.minimal,
            ZetaWidgetBorder.full => zeta.radius.full,
            _ => zeta.radius.none,
          };

          final defaultInputBorder = OutlineInputBorder(
            borderRadius: borderRadius,
            borderSide: BorderSide(color: zeta.colors.cool.shade40),
          );

          final focusedBorder = defaultInputBorder.copyWith(
            borderSide: BorderSide(
              color: zeta.colors.blue.shade50,
              width: zeta.spacing.minimum,
            ),
          );

          final disabledborder = defaultInputBorder.copyWith(
            borderSide: BorderSide(color: zeta.colors.borderDisabled),
          );

          late final double iconSize;
          late final double padding;

          switch (size) {
            case ZetaWidgetSize.large:
              iconSize = zeta.spacing.xl_2;
              padding = zeta.spacing.medium;
            case ZetaWidgetSize.medium:
              iconSize = zeta.spacing.xl;
              padding = zeta.spacing.small;
            case ZetaWidgetSize.small:
              iconSize = zeta.spacing.large;
              padding = zeta.spacing.minimum;
          }

          return ZetaRoundedScope(
            rounded: shape != ZetaWidgetBorder.sharp,
            child: Semantics(
              excludeSemantics: disabled,
              label: disabled ? hintText ?? 'Search' : null, // TODO(UX-1003): Localize
              enabled: disabled ? false : null,
              child: TextFormField(
                focusNode: focusNode,
                enabled: !disabled,
                controller: state.effectiveController,
                keyboardType: TextInputType.text,
                textInputAction: textInputAction,
                onFieldSubmitted: onFieldSubmitted,
                onChanged: state.onChange,
                style: ZetaTextStyles.bodyMedium,
                decoration: InputDecoration(
                  isDense: true,
                  contentPadding: EdgeInsets.symmetric(
                    horizontal: 10,
                    vertical: padding,
                  ),
                  hintText: hintText ?? 'Search', // TODO(UX-1003): Localize
                  hintStyle: ZetaTextStyles.bodyMedium.copyWith(
                    color: !disabled ? zeta.colors.textSubtle : zeta.colors.textDisabled,
                  ),
                  prefixIcon: Padding(
                    padding: EdgeInsets.only(left: zeta.spacing.medium, right: zeta.spacing.small),
                    child: ZetaIcon(
                      ZetaIcons.search,
                      color: !disabled ? zeta.colors.cool.shade70 : zeta.colors.cool.shade50,
                      size: iconSize,
                    ),
                  ),
                  prefixIconConstraints: BoxConstraints(
                    minHeight: zeta.spacing.xl_2,
                    minWidth: zeta.spacing.xl_2,
                  ),
                  suffixIcon: IntrinsicHeight(
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        if (state.effectiveController.text.isNotEmpty && !disabled) ...[
                          Semantics(
                            container: true,
                            button: true,
                            excludeSemantics: true,
                            label: clearSemanticLabel,
                            child: InputIconButton(
                              icon: ZetaIcons.cancel,
                              onTap: () => state.onChange(''),
                              disabled: disabled,
                              size: size,
                              color: zeta.colors.iconSubtle,
                              key: const ValueKey('search-clear-btn'),
                            ),
                          ),
                          if (showSpeechToText)
                            SizedBox(
                              height: iconSize,
                              child: VerticalDivider(
                                color: zeta.colors.cool.shade40,
                                width: 5,
                                thickness: 1,
                              ),
                            ),
                        ],
                        if (showSpeechToText)
                          Semantics(
                            label: microphoneSemanticLabel,
                            container: true,
                            button: true,
                            excludeSemantics: true,
                            child: InputIconButton(
                              icon: ZetaIcons.microphone,
                              onTap: state.onSpeechToText,
                              key: const ValueKey('speech-to-text-btn'),
                              disabled: disabled,
                              size: size,
                              color: zeta.colors.iconDefault,
                            ),
                          ),
                      ],
                    ),
                  ),
                  suffixIconConstraints: BoxConstraints(
                    minHeight: zeta.spacing.xl_2,
                    minWidth: zeta.spacing.xl_2,
                  ),
                  filled: !disabled ? null : true,
                  fillColor: !disabled ? null : zeta.colors.cool.shade30,
                  enabledBorder: defaultInputBorder,
                  focusedBorder: focusedBorder,
                  disabledBorder: disabledborder,
                ),
              ),
            ),
          );
        },
      );