library;

import 'package:flutter/material.dart';
import 'package:get/get.dart';

class PopupReply extends StatefulWidget {
  const PopupReply({
    super.key,
    this.onChanged,
    this.onSubmitted, // 添加 onSubmitted 参数
    this.hintText = '说点什么...',
  });

  // 输入框值改变
  final ValueChanged<String>? onChanged;
  final ValueChanged<String>? onSubmitted; // 定义 onSubmitted
  final String hintText;

  @override
  State<PopupReply> createState() => _PopupReplyState();
}

class _PopupReplyState extends State<PopupReply> {
  final TextEditingController controller = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    controller.dispose();
  }

  // 处理提交事件
  void _handleSubmit() {
    final text = controller.text.trim();
    if (text.isNotEmpty) {
      // 优先调用 onSubmitted
      if (widget.onSubmitted != null) {
        widget.onSubmitted!(text);
      } else if (widget.onChanged != null) {
        // 如果没有 onSubmitted，则使用 onChanged
        widget.onChanged!(text);
        Get.back();
      }
    }
  }

  // 处理 onSubmitted 回调（接收 String 参数）
  void _onSubmitted(String value) {
    _handleSubmit();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: GestureDetector(
                child: Container(
                  color: Colors.transparent,
                ),
                onTap: () {
                  Get.back();
                },
              ),
            ),
            Container(
              color: Colors.white,
              padding: EdgeInsets.all(10.0),
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      margin: EdgeInsets.only(right: 10.0),
                      decoration: BoxDecoration(
                        color: Colors.grey[200],
                        borderRadius: BorderRadius.circular(20.0),
                      ),
                      child: TextField(
                        decoration: InputDecoration(
                          hintText: widget.hintText,
                          isDense: true,
                          hoverColor: Colors.transparent,
                          contentPadding: EdgeInsets.all(10.0),
                          border: OutlineInputBorder(borderSide: BorderSide.none),
                        ),
                        style: TextStyle(fontSize: 14.0,),
                        textInputAction: TextInputAction.send,
                        autofocus: true,
                        maxLines: null,
                        controller: controller,
                        cursorColor: Color(0xFFFF5000),
                        onEditingComplete: _handleSubmit, // 使用统一的提交处理
                        onChanged: widget.onChanged, // 直接传递 onChanged
                        onSubmitted: _onSubmitted, // 传递正确的函数签名
                      ),
                    ),
                  ),
                  FilledButton(
                    style: ButtonStyle(
                      backgroundColor: WidgetStateProperty.all(Color(0xFFFF5000)),
                      padding: WidgetStateProperty.all(EdgeInsets.zero),
                      minimumSize: WidgetStateProperty.all(Size(60.0, 40.0)),
                      shape: WidgetStatePropertyAll(
                        RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0))
                      )
                    ),
                    onPressed: _handleSubmit, // 使用统一的提交处理
                    child: Text('发送',),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}