buildTextField method

Widget buildTextField(
  1. HMTextFieldType type,
  2. TextEditingController controller,
  3. HMTextFieldSize fieldSize,
  4. HMTextVariant fieldVariant,
  5. HMRadius fieldRadius,
  6. Color fieldIconColor,
  7. Color background,
)

Implementation

Widget buildTextField(
    HMTextFieldType type,
    TextEditingController controller,
    HMTextFieldSize fieldSize,
    HMTextVariant fieldVariant,
    HMRadius fieldRadius,
    Color fieldIconColor,
    Color background) {
  final textSize = _getTextSize(fieldSize);
  final showPassword = useState(false);
  switch (type) {
    case HMTextFieldType.text:
      return Container(
        // padding: const EdgeInsets.symmetric(vertical: 0),
        height: fieldSize.value,
        decoration: BoxDecoration(
          color: disabled
              ? const Color(0x16000000)
              : fieldVariant == HMTextVariant.filled
                  ? background
                  : null,
          border: Border.all(
              color: disabled
                  ? const Color(0x00000000)
                  : borderColor ?? outlineColor),
          borderRadius: BorderRadius.circular(fieldRadius.value),
        ),
        child: Row(
          children: <Widget>[
            if (prefixIcon != null)
              _buildPrefixIcon(_getTextSize(fieldSize))
            else
              const SizedBox(),
            Expanded(
              child: TextField(
                keyboardType: TextInputType.text,
                textInputAction: textInputAction,
                controller: controller,
                focusNode: focusNode,
                style: TextStyle(fontSize: textSize, height: 1.5),
                textCapitalization: TextCapitalization.sentences,
                autocorrect: !(keyboardType == TextInputType.emailAddress),
                maxLength: maxLength,
                inputFormatters: inputFormatters,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  counterText: '',
                  hintText: hintText,
                  hintStyle:
                      TextStyle(color: Colors.grey, fontSize: textSize),
                  contentPadding: const EdgeInsets.symmetric(horizontal: 5),
                  isDense: true,
                ),
                onTap: onTap,
                onChanged: onChange,
                onSubmitted: onSubmitted,
                onEditingComplete: onEditingComplete,
              ),
            ),
            if (suffixIcon != null)
              _buildSuffixIcon(_getTextSize(fieldSize))
            else
              const SizedBox(),
          ],
        ),
      );

    case HMTextFieldType.password:
      return Container(
        height: fieldSize.value,
        decoration: BoxDecoration(
          color: disabled
              ? const Color(0x16000000)
              : fieldVariant == HMTextVariant.filled
                  ? background
                  : null,
          border: Border.all(
              color: disabled
                  ? const Color(0x00000000)
                  : borderColor ?? outlineColor),
          borderRadius: BorderRadius.circular(fieldRadius.value),
        ),
        child: Row(
          children: <Widget>[
            if (prefixIcon != null)
              _buildPrefixIcon(_getTextSize(fieldSize))
            else
              const SizedBox(),
            Expanded(
              child: TextField(
                keyboardType: TextInputType.visiblePassword,
                controller: controller,
                style: TextStyle(fontSize: textSize, height: 1.5),
                obscureText: !showPassword.value,
                textInputAction: textInputAction,
                autocorrect: false,
                focusNode: focusNode,
                maxLength: maxLength,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  enabled: !disabled,
                  counterText: '',
                  hintText: hintText,
                  hintStyle: const TextStyle(color: Colors.grey),
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
                  isDense: true,
                ),
                onTap: onTap,
                onChanged: onChange,
                onSubmitted: onSubmitted,
                onEditingComplete: onEditingComplete,
              ),
            ),
            GestureDetector(
              child: Padding(
                padding: const EdgeInsets.only(right: 10.0),
                child: Container(
                    constraints: BoxConstraints(minHeight: textSize * 2),
                    child: Icon(
                      showPassword.value
                          ? hidePasswordIcon ?? Icons.visibility_off
                          : showPasswordIcon ?? Icons.visibility,
                      size: textSize * 1.5,
                      color: Colors.grey,
                    )),
              ),
              onTap: () {
                showPassword.value = !showPassword.value;
              },
            ),
          ],
        ),
      );

    case HMTextFieldType.number:
      return Container(
        height: fieldSize.value,
        decoration: BoxDecoration(
          color: disabled
              ? const Color(0x16000000)
              : fieldVariant == HMTextVariant.filled
                  ? background
                  : null,
          border: Border.all(
              color: disabled
                  ? const Color(0x00000000)
                  : borderColor ?? outlineColor),
          borderRadius: BorderRadius.circular(fieldRadius.value),
        ),
        child: Row(
          children: <Widget>[
            if (prefixIcon != null)
              _buildPrefixIcon(_getTextSize(fieldSize))
            else
              const SizedBox(),
            Expanded(
              child: TextField(
                controller: controller,
                style: TextStyle(fontSize: textSize, height: 1.5),
                textInputAction: textInputAction,
                keyboardType: TextInputType.number,
                autocorrect: false,
                focusNode: focusNode,
                maxLength: maxLength,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  enabled: !disabled,
                  hintText: hintText,
                  hintStyle: const TextStyle(color: Colors.grey),
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
                  isDense: true,
                  counterText: '',
                ),
                inputFormatters: [
                  FilteringTextInputFormatter.allow(
                      RegExp(r'[0-9]+[,.]{0,1}[0-9]*')),
                  TextInputFormatter.withFunction(
                    (oldValue, newValue) => newValue.copyWith(
                      text: newValue.text.replaceAll(',', '.'),
                    ),
                  ),
                  ...?inputFormatters,
                ],
                onTap: onTap,
                onChanged: onChange,
                onSubmitted: onSubmitted,
                onEditingComplete: onEditingComplete,
              ),
            ),
          ],
        ),
      );

    case HMTextFieldType.multiline:
      return Container(
        height: fieldSize.value,
        decoration: BoxDecoration(
          color: disabled
              ? const Color(0x16000000)
              : fieldVariant == HMTextVariant.filled
                  ? background
                  : null,
          border: Border.all(
              color: disabled
                  ? const Color(0x00000000)
                  : borderColor ?? outlineColor),
          borderRadius: BorderRadius.circular(fieldRadius.value),
        ),
        child: Row(
          children: <Widget>[
            if (prefixIcon != null)
              _buildPrefixIcon(_getTextSize(fieldSize))
            else
              const SizedBox(),
            Expanded(
              child: TextField(
                controller: controller,
                style: TextStyle(fontSize: textSize, height: 1.5),
                keyboardType: TextInputType.multiline,
                textCapitalization: TextCapitalization.sentences,
                textInputAction: textInputAction,
                minLines: minLines,
                focusNode: focusNode,
                maxLines: maxLength,
                inputFormatters: inputFormatters,
                decoration: InputDecoration(
                  border: InputBorder.none,
                  hintText: hintText,
                  hintStyle: const TextStyle(color: Colors.grey),
                  contentPadding:
                      const EdgeInsets.symmetric(vertical: 8, horizontal: 5),
                  isDense: true,
                ),
                onTap: onTap,
                onChanged: onChange,
                onSubmitted: onSubmitted,
                onEditingComplete: onEditingComplete,
              ),
            ),
            if (suffixIcon != null)
              _buildSuffixIcon(_getTextSize(fieldSize))
            else
              Container(),
          ],
        ),
      );
  }
}