part of 'field.dart';

class AppPasswordTextFormField extends StatefulWidget {
  const AppPasswordTextFormField({
    super.key,
    this.hintText,
    required this.title,
    this.controller,
    this.focusNode,
    this.prefixIcon,
    this.keyboardType,
    this.onChanged,
    this.validator,
  });

  final String? hintText;
  final String title;
  final TextEditingController? controller;
  final FocusNode? focusNode;
  final Widget? prefixIcon;
  final TextInputType? keyboardType;
  final ValueChanged<String>? onChanged;
  final String? Function(String?)? validator;

  @override
  State<AppPasswordTextFormField> createState() =>
      _AppPasswordTextFormFieldState();
}

class _AppPasswordTextFormFieldState extends State<AppPasswordTextFormField> {
  bool isPasswordVisible = true;
  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          widget.title,
          style: AppStyle.lg.copyWith(fontWeight: FontWeight.w600),
        ),
        const SizedBox(height: 8),
        TextFormField(
          controller: widget.controller,
          focusNode: widget.focusNode,
          keyboardType: widget.keyboardType,
          onChanged: widget.onChanged,
          cursorColor: AppColor.primary,
          obscureText: isPasswordVisible,
          style: AppStyle.md.copyWith(
            color: AppColor.textPrimary,
            fontWeight: FontWeight.w500,
          ),
          validator: widget.validator,
          decoration: InputDecoration(
            hintText: widget.hintText,
            prefixIcon: widget.prefixIcon,
            suffixIcon: isPasswordVisible
                ? IconButton(
                    icon: Icon(
                      Icons.visibility,
                      color: AppColor.primary,
                      size: 20,
                    ),
                    constraints: const BoxConstraints(),
                    padding: const EdgeInsets.all(8),
                    onPressed: () {
                      setState(() {
                        isPasswordVisible = !isPasswordVisible;
                      });
                    },
                  )
                : IconButton(
                    icon: Icon(
                      Icons.visibility_off,
                      color: AppColor.primary,
                      size: 20,
                    ),
                    constraints: const BoxConstraints(),
                    padding: const EdgeInsets.all(8),
                    onPressed: () {
                      setState(() {
                        isPasswordVisible = !isPasswordVisible;
                      });
                    },
                  ),

            prefixIconConstraints: const BoxConstraints(
              minWidth: 0,
              minHeight: 0,
            ),
            suffixIconConstraints: const BoxConstraints(
              minWidth: 0,
              minHeight: 0,
            ),
          ),
        ),
      ],
    );
  }
}
