import 'package:base_app/res/index.dart';
import 'package:flutter/material.dart';

/// 登录项
class LoginItem extends StatefulWidget {
  final IconData prefixIcon; // 前方icon
  final bool hasSuffixIcon; // 是否要后面的icon
  final String hintText; // 提示文字
  final bool autoFocus; // 自动聚焦
  final TextInputType keyboardType; // 键盘类型
  final int maxLength; // 最大程度
  final bool maxLengthEnforced; // 当长度达到限制长度时，是否强制组织输入
  final TextInputAction textInputAction; // 输入action
  final TextEditingController controller; // controller
  final FocusNode focusNode; //focusNode
  final VoidCallback onEditingComplete; // 输入完毕后的回调

  const LoginItem(
      {Key key,
      this.prefixIcon,
      this.hasSuffixIcon = false,
      this.autoFocus = false,
      this.keyboardType = TextInputType.text,
      this.hintText,
      this.maxLength,
      this.maxLengthEnforced = true,
      this.textInputAction,
      this.controller,
      this.focusNode,
      this.onEditingComplete})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return LoginItemState();
  }
}

class LoginItemState extends State<LoginItem> {
  bool _obscureText; // 是否隐藏原始文字 // 密码用

  @override
  void initState() {
    super.initState();
    _obscureText = widget.hasSuffixIcon;
  }

  @override
  Widget build(BuildContext context) {
    return new Row(
      children: <Widget>[
        new IconButton(
            iconSize: 28,
            icon: new Icon(
              widget.prefixIcon,
              color: Theme.of(context).primaryColor,
            ),
            onPressed: () {}),
        Gaps.hGap5,
        new Expanded(
          child: new TextField(
              textInputAction: widget.textInputAction,
              focusNode: widget.focusNode,
              onEditingComplete: widget.onEditingComplete,
              obscureText: _obscureText,
              controller: widget.controller,
              maxLength: widget.maxLength,
              maxLengthEnforced: widget.maxLengthEnforced,
              style: new TextStyle(color: Colours.gray_66, fontSize: 14),
              decoration: new InputDecoration(
                hintText: widget.hintText,
                hintStyle: new TextStyle(color: Colours.gray_99, fontSize: 14),
                suffixIcon: widget.hasSuffixIcon
                    ? new IconButton(
                        icon: new Icon(
                          _obscureText
                              ? Icons.visibility
                              : Icons.visibility_off,
                          color: Colours.gray_66,
                        ),
                        onPressed: () {
                          setState(() {
                            _obscureText = !_obscureText;
                          });
                        })
                    : null,
                focusedBorder: new UnderlineInputBorder(
                    borderSide: new BorderSide(color: Colours.green_de)),
                enabledBorder: new UnderlineInputBorder(
                    borderSide: new BorderSide(color: Colours.green_de)),
              )),
        ),
      ],
    );
  }
}
