import 'package:e_commerce_training/res/R.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class TextFieldCustom extends StatefulWidget {
  const TextFieldCustom(
      {required this.controller,
//      this.key,
      this.textInputAction: TextInputAction.next,
      this.isEnable = true,
      this.autoFocus = true,
      this.onChanged,
      this.isPassword: false,
      this.icon,
      this.errorText,
      this.labelText,
      this.hintText,
      this.inputFormatters,
      this.maxLines,
      this.keyboardType: TextInputType.text,
      this.focusNode,
      this.readOnly: false,
      this.onTap,
      this.onSubmitted});

//  final GlobalKey key;
  final TextEditingController controller;
  final bool isEnable;
  final bool autoFocus;
  final List<TextInputFormatter>? inputFormatters;
  final TextInputAction textInputAction;
  final FormFieldSetter<String>? onChanged;
  final bool isPassword;
  final String? errorText;
  final String? labelText;
  final String? hintText;
  final int? maxLines;
  final TextInputType keyboardType;
  final FocusNode? focusNode;
  final FormFieldSetter<String>? onSubmitted;
  final dynamic icon;
  final bool readOnly;
  final Function? onTap;

  @override
  _TextFieldCustomState createState() => _TextFieldCustomState();
}

class _TextFieldCustomState extends State<TextFieldCustom> {
  late bool _obscureText;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _obscureText = widget.isPassword;
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
//      key: widget.key,
      enabled: widget.isEnable,
      autofocus: widget.autoFocus,
      focusNode: widget.focusNode,
      controller: widget.controller,
      obscureText: _obscureText,
      textInputAction: widget.textInputAction,
      inputFormatters: widget.inputFormatters,
      readOnly: widget.readOnly,
      //onTap: widget.onTap,
      decoration: InputDecoration(
          contentPadding: EdgeInsets.symmetric(vertical: 20, horizontal: 15),
          hintText: widget.hintText,
          labelText: widget.labelText ?? null,
          errorText: widget.errorText,
          border: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(5.0)),
              borderSide: BorderSide(color: R.color.gray, width: 1)),
          prefixIcon: widget.icon == null
              ? null
              : (widget.icon is String
                  ? Padding(
                      child: Image.asset(
                        widget.icon,
                        fit: BoxFit.fitHeight,
                        height: 3,
                      ),
                      padding: EdgeInsets.all(16),
                    )
                  : Icon(
                      widget.icon,
                      size: 30.h,
                    )),
          labelStyle: TextStyle(
            fontSize: 16.sp,
            color: widget.isEnable ? R.color.black : R.color.gray,
          ),
          hintStyle: TextStyle(
            fontSize: 16.sp,
            color: R.color.gray,
          ),
          errorStyle: TextStyle(
            fontSize: 16.sp,
            color: R.color.red,
          )),
      keyboardType: widget.keyboardType,
      onChanged: widget.onChanged,
      onSubmitted: widget.onSubmitted,
      maxLines: widget.isPassword == true ? 1 : widget.maxLines,
      textAlign: TextAlign.start,
      textAlignVertical: TextAlignVertical.center,
      style: TextStyle(
        fontSize: 16.sp,
        color: widget.isEnable ? R.color.black : R.color.gray,
      ),
    );
  }
}
