import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hyzp_ybqx/components/commonFun.dart';

class JdText extends StatefulWidget {
  final String text;
  final String title;
  bool password;
  final Function(String) onChanged;
  final int maxLines;
  final double height;
  TextEditingController controller;
  final String endBtn;
  final Function() onEndBtn;
  final Function() onEndBtn2;

  JdText(
      {Key key,
      this.text = "输入内容",
      this.title = '标题',
      this.password = false,
      this.onChanged,
      this.maxLines = 1,
      this.height = 68,
      this.controller,
      this.endBtn,
      this.onEndBtn,
      this.onEndBtn2})
      : super(key: key);

  _JdTextState createState() => _JdTextState();
}

class _JdTextState extends State<JdText> {
  Icon ShowHiddenIcon = Icon(Icons.more_horiz);

  // Flutter 监听TextField焦点
  FocusNode _focusNode = FocusNode();
  bool isFocus = false;

  void initState() {
    super.initState();

    // _focusNode.addListener(() {
    //   setState(() {
    //     isFocus = _focusNode.hasFocus;
    //     widget.onEndBtn2();
    //   });
    // });

    //输入框焦点
    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {
        print(widget.endBtn + '：失去焦点');
      } else {
        print(widget.endBtn + '：得到焦点');
        if (null != widget.onEndBtn2) {
          widget.onEndBtn2();
          setState(() {});
        }
      }
    });
  }

  //离开页面记着销毁和清除
  @override
  void dispose() {
    // TODO: implement dispose
    _focusNode.unfocus();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: Alignment(0, -1),
      padding: EdgeInsets.only(top: 0),
      height: ScreenUtil().setHeight(widget.height),
      decoration:
          BoxDecoration(border: Border(bottom: BorderSide(width: 1, color: Colors.black12))),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Container(
            alignment: Alignment(0, 0),
            child: Container(
              padding: EdgeInsets.only(top: 0, bottom: 0),
              child: Text(
                widget.title,
                style: TextStyle(
                  fontSize: 17,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),
          //Flutter中Row中不能直接使用textfield控件
          Expanded(
            child: TextField(
              focusNode: _focusNode,
              textAlignVertical: TextAlignVertical(y: 1.0),
              controller: widget.controller,
              maxLines: 1,
              obscureText: widget.password,
              decoration: InputDecoration(
                //contentPadding: EdgeInsets.only(bottom: 16),
                hintText: widget.text,
                border: OutlineInputBorder(
                    borderRadius: BorderRadius.circular(30), borderSide: BorderSide.none),
              ),
              onChanged: widget.onChanged,
            ),
          ),
          getBtn(),
        ],
      ),
    );
  }

  Widget getBtn() {
    Widget btn;
    switch (widget.endBtn) {
      case 'ClearBtn':
        widget.controller = new TextEditingController();
        btn = Container(
          // alignment: Alignment(0, -1),
          // padding: EdgeInsets.only(bottom: 23),
          child: IconButton(
              padding: EdgeInsets.only(bottom: 0.0),
              iconSize: 40,
              icon: g_can_expand_ListView
                  ? Icon(
                      Icons.arrow_drop_up,
                      color: Colors.blueAccent,
                    )
                  : Icon(
                      Icons.arrow_drop_down,
                      color: g_users.isNotEmpty ? Colors.blueAccent : Colors.grey,
                    ),
              onPressed: widget.onEndBtn),
        );
        break;
      // case 'ClearBtn':
      //   widget.controller = new TextEditingController();
      //   btn = Container(
      //     // alignment: Alignment(0, -1),
      //     // padding: EdgeInsets.only(bottom: 23),
      //     child: IconButton(
      //         icon: Icon(Icons.highlight_off),
      //         onPressed: () {
      //           setState(() {
      //             widget.controller.clear();
      //           });
      //         }),
      //   );
      //   break;
      case 'ShowHiddenBtn':
        btn = Container(
          // alignment: Alignment(0, -1),
          // padding: EdgeInsets.only(bottom: 23),
          child: IconButton(
              icon: ShowHiddenIcon,
              onPressed: () {
                widget.password = !widget.password;
                setState(() {
                  ShowHiddenIcon =
                      widget.password ? Icon(Icons.more_horiz) : Icon(Icons.remove_red_eye);
                });
              }),
        );
        break;
      case 'OutlineButton':
        btn = Container(
          child: OutlineButton(
            borderSide: BorderSide(color: Colors.blue),
            onPressed: () {},
            child: Container(
              child: Text("获取验证码"),
            ),
          ),
        );
        break;
      default:
        btn = SizedBox.shrink();
        break;
    }
    return btn;
  }
}
