import 'package:flutter/material.dart';
import 'package:sk_base_mobile/app_theme.dart';
import 'package:sk_base_mobile/util/screen_adaper_util.dart';

class SkTextInput extends StatefulWidget {
  final TextEditingController textController;
  final Function(FocusNode)? onTap;
  final bool isRequired;
  final String labelText;
  final String? hint;
  final bool isTextArea;
  final bool isDense;
  final EdgeInsetsGeometry? contentPadding;
  final bool autoFocus;
  SkTextInput({
    super.key,
    required this.textController,
    this.onTap,
    this.hint,
    this.isRequired = false,
    this.labelText = '',
    this.isTextArea = false,
    this.autoFocus = false,
    this.contentPadding,
    this.isDense = false,
  });

  @override
  State<SkTextInput> createState() => _SkTextInputState();
}

class _SkTextInputState extends State<SkTextInput> {
  late FocusNode focusNode = FocusNode();
  @override
  void initState() {
    super.initState();
    if (widget.autoFocus) {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        focusNode.requestFocus();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      focusNode: focusNode,
      controller: widget.textController,
      onTapOutside: (event) {
        FocusScope.of(context).unfocus();
      },
      maxLines: widget.isTextArea ? 2 : 1, // 添加这行代码
      onTap: () {
        if (widget.onTap == null) {
          widget.onTap!(focusNode);
        }
      },
      decoration: InputDecoration(
        contentPadding: widget.contentPadding,
        isDense: widget.isDense,
        floatingLabelBehavior: FloatingLabelBehavior.always,
        label: Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              if (widget.isRequired)
                Text(
                  "*",
                  style: TextStyle(
                      color: Colors.red, fontSize: ScreenAdaper.height(30)),
                ),
              Text(
                widget.labelText,
                style: TextStyle(fontSize: ScreenAdaper.height(30)),
              ),
            ]),
        focusedBorder: OutlineInputBorder(
            borderSide:
                const BorderSide(color: AppTheme.primaryColorLight, width: 2),
            borderRadius: BorderRadius.circular(ScreenAdaper.sp(15))),
        hintText: widget.hint ?? '请输入',
      ),
    );
  }
}
