import 'package:flutter/material.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';
import 'package:sk_base_mobile/widgets/form_item/sk_form_item.dart';
import 'package:sk_base_mobile/widgets/form_item/sk_base_field.dart';

class SkTextInput extends SkBaseFieldWidget {
  final TextEditingController textController;
  final Function(FocusNode)? onTap;
  final bool isRequired;

  final String? hint;
  final bool isTextArea;
  final bool isDense;
  final Function(String)? onTapOutside;
  final Function(String)? onChanged;
  final String? Function(String?)? validator;
  final EdgeInsetsGeometry? contentPadding;

  final ValueChanged<String>? onFieldSubmitted;
  final Icon? prefix;
  final Widget? suffixIcon;
  final InputBorder? border;
  final FloatingLabelBehavior? floatingLabelBehavior;
  final TextInputType? keyboardType;

  SkTextInput(
      {super.key,
      super.customLabel = false,
      super.autoFocus = false,
      super.labelText,
      super.fillColor,
      required this.textController,
      this.onTap,
      this.hint,
      this.onFieldSubmitted,
      this.isRequired = false,
      this.onTapOutside,
      this.keyboardType,
      this.prefix,
      this.suffixIcon,
      this.onChanged,
      this.border,
      this.floatingLabelBehavior = FloatingLabelBehavior.always,
      this.isTextArea = false,
      this.contentPadding,
      this.isDense = false,
      this.validator});

  @override
  Widget build(BuildContext context) {
    Widget field = TextFormField(
      focusNode: focusNode,
      controller: textController,
      onChanged: (String value) {
        if (onChanged != null) {
          onChanged!(value);
        }
      },
      onTapOutside: (event) {
        if (onTapOutside != null) {
          onTapOutside!(textController.text);
          FocusScope.of(context).unfocus();
        }
      },
      maxLines: isTextArea ? 2 : 1, // 添加这行代码
      onTap: () {
        if (onTap != null) {
          onTap!(focusNode);
        }
      },
      keyboardType: keyboardType,
      onFieldSubmitted: onFieldSubmitted,
      autovalidateMode: AutovalidateMode.onUserInteraction,
      validator: validator,

      decoration: InputDecoration(
        prefixIcon: prefix,
        suffixIcon: suffixIcon,
        fillColor: fillColor,
        filled: true,
        errorStyle: const TextStyle(fontSize: 0, height: 0.01),
        contentPadding: contentPadding,
        isDense: isDense,
        border: border,
        floatingLabelBehavior: floatingLabelBehavior,
        label: labelText != null && !customLabel
            ? Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisSize: MainAxisSize.min,
                children: [
                    if (isRequired)
                      Text(
                        "*",
                        style: TextStyle(
                            color: Colors.red,
                            fontSize: ScreenAdaper.height(30)),
                      ),
                    Text(
                      labelText!,
                      style: TextStyle(fontSize: ScreenAdaper.height(30)),
                    ),
                  ])
            : null,
        hintText: hint ?? '请输入',
      ),
    );
    return SkFormItem(
      controller: baseFieldController,
      customLabel: customLabel,
      labelText: labelText,
      isRequired: isRequired,
      child: field,
    );
  }
}

// class SkTextInputController extends GetxController {
//   RxBool isFocus = false.obs;
//   @override
//   void onReady() {
//     super.onReady();
//   }
// }
