import 'package:flutter/material.dart';
import 'package:test_sa/views/app_style/sizing.dart';class ATextFormField extends StatefulWidget {
  final Function(String)? onSaved;
  final String? Function(String?)? validator;
  final Function(String)? onChange;
  final bool? obscureText;
  final VoidCallback? showPassword;
  final String? hintText;final String? labelText;
  final TextInputType textInputType;
  final String? initialValue;
  final TextStyle? style;
  final bool enable;
  final TextAlign? textAlign;
  final FocusNode? node;
  final Widget? suffixIcon;
  final IconData? prefixIconData;
  final double? prefixIconSize;
  final TextEditingController? controller;
  final TextInputAction? textInputAction;
  final VoidCallback? onAction;

  const ATextFormField({
    Key? key,
    this.onSaved,
    this.validator,this.node,
    this.onChange,
    this.obscureText,
    this.showPassword,
    this.hintText,
    this.labelText,
    this.textInputType = TextInputType.text,
    this.initialValue,
    this.enable = true,
    this.style,
    this.textAlign,
    this.suffixIcon,
    this.prefixIconData,
    this.prefixIconSize,
    this.controller,
    this.textInputAction,
    this.onAction,
  }) : super(key: key);

  @override
  State<ATextFormField> createState() => _ATextFormFieldState();
}

class _ATextFormFieldState extends State<ATextFormField> {
  @override
  void initState() {
    if (widget.controller != null && widget.initialValue != null) {
      widget.controller!.text = widget.initialValue!;
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.textInputType == TextInputType.multiline ? null : 50,padding: const EdgeInsets.only(left: 12, right: 12),
      decoration: BoxDecoration(
        color: const Color(0xfff5f5f5),
        border: Border.all(
          color: const Color(0xffefefef),
        ),
        borderRadius: BorderRadius.circular(AppStyle.borderRadius * AppStyle.getScaleFactor(context)),
      ),
      child: TextFormField(
        focusNode: widget.node,
        enabled: widget.enable,
        //onSaved: widget.onSaved,
        initialValue: widget.controller != null ? null : widget.initialValue,
        validator: widget.validator,
        onChanged: widget.onChange,
        textAlign: widget.textAlign ?? TextAlign.left,
        obscureText: widget.obscureText ?? false,
        keyboardType: widget.textInputType,
        maxLines: widget.textInputType == TextInputType.multiline ? null : 1,
        obscuringCharacter: "●",
        controller: widget.controller,
        textInputAction: widget.textInputType == TextInputType.multiline ? null : widget.textInputAction ?? TextInputAction.next,
        onEditingComplete: widget.onAction ?? () => FocusScope.of(context).nextFocus(),
        style: widget.style ?? Theme.of(context).textTheme.bodyLarge,
        decoration: InputDecoration(
          border: InputBorder.none,
          suffixIconConstraints: const BoxConstraints(minWidth: 0),
          disabledBorder: InputBorder.none,
          focusedBorder: InputBorder.none,
          enabledBorder: InputBorder.none,
          constraints: const BoxConstraints(),
          errorStyle: const TextStyle(height: 0.3),
          hintText: widget.hintText,
          labelText: widget.labelText,
          suffixIcon: widget.suffixIcon,
          prefixIcon: widget.prefixIconData == null
              ? null
              : Icon(
            widget.prefixIconData,
            size: widget.prefixIconSize == null ? 20 * AppStyle.getScaleFactor(context) : (widget.prefixIconSize! - 10) * AppStyle.getScaleFactor(context),
            color: const Color(0xff2e303a),
          ),
        ),
      ),
    );
  }
}