import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class SecurityBindInput extends StatefulWidget {
  final String title;
  final Color titleColor;
  final String placeholder;
  final Color placeholderColor;
  final Widget rightWidget;
  final TextInputType keyboardType;
  final List<TextInputFormatter> inputFormatters;
  final TextEditingController controller;
  final bool enabled;
  final bool obscureText; // 是否密码

  const SecurityBindInput(
    this.title,
    this.placeholder, {
    Key key,
    this.rightWidget,
    this.titleColor,
    this.placeholderColor,
    this.keyboardType,
    this.inputFormatters,
    this.controller,
    this.enabled = true,
    this.obscureText = false,
  }) : super(key: key);

  @override
  _SecurityBindInputState createState() => _SecurityBindInputState();
}

class _SecurityBindInputState extends State<SecurityBindInput> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: double.infinity,
      padding: EdgeInsets.only(left: 12.5, right: 12.5),
      height: 50,
      child: Row(
        children: <Widget>[
          _createTitle(),
          _createTextField(),
          widget.rightWidget ?? Container(),
        ],
      ),
    );
  }

  Widget _createTitle() {
    return Container(
      width: 80,
      child: Text(
        widget.title ?? '',
        style: TextStyle(
          fontSize: 13,
          fontWeight: FontWeight.bold,
        ),
      ),
    );
  }

  Widget _createTextField() {
    return Expanded(
      child: CupertinoTextField(
        placeholder: widget.placeholder,
        placeholderStyle: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.bold,
            color: widget.placeholderColor ?? Color(0xff999999)),
        decoration: BoxDecoration(color: Colors.transparent),
        style: TextStyle(
          color: widget.titleColor ?? Color(0xff333333),
          fontSize: 13,
        ),
        keyboardType: widget.keyboardType,
        inputFormatters: widget.inputFormatters,
        controller: widget.controller,
        onSubmitted: (_) {
          FocusScope.of(context).requestFocus(FocusNode());
        },
        enabled: widget.enabled,
        obscureText: widget.obscureText,
      ),
    );
  }
}
