editPassword method

Widget editPassword({
  1. Key? key,
  2. required String name,
  3. required VoidCallback onUpdateState,
  4. String? initialValue,
  5. int minLength = 1,
  6. int maxLength = 128,
  7. String? label = "密码",
  8. String? errorText,
  9. String? hint,
  10. bool allowEmpty = false,
  11. TextValidator? validator,
  12. OnValue<String>? onChanged,
  13. OnValue<String>? onSubmitted,
  14. List<TextInputFormatter>? inputFormatters,
  15. Color? cursorColor,
  16. InputBorder? border,
  17. Widget? prefixIcon = const Icon(Icons.lock),
  18. TextInputAction? textInputAction = TextInputAction.done,
})

Implementation

Widget editPassword({
  Key? key,
  required String name,
  required VoidCallback onUpdateState,
  String? initialValue,
  int minLength = 1,
  int maxLength = 128,
  String? label = "密码",
  String? errorText,
  String? hint,
  bool allowEmpty = false,
  TextValidator? validator,
  OnValue<String>? onChanged,
  OnValue<String>? onSubmitted,
  List<TextInputFormatter>? inputFormatters,
  Color? cursorColor,
  InputBorder? border,
  Widget? prefixIcon = const Icon(Icons.lock),
  TextInputAction? textInputAction = TextInputAction.done,
}) {
  _nameControllerMap[name] ??= TextEditingController(text: initialValue);
  _focusMap[name] ??= FocusNode();
  _eyeMap[name] ??= true;

  var lv = LengthValidator(minLength: minLength, maxLength: maxLength, allowEmpty: allowEmpty);
  TextValidator tv = validator == null ? lv : ListValidator([validator, lv]);

  return TextFormField(
    key: key ?? UniqueKey(),
    initialValue: null,
    controller: _nameControllerMap[name],
    cursorColor: cursorColor,
    obscureText: _eyeMap[name]!,
    maxLength: maxLength,
    keyboardType: TextInputType.visiblePassword,
    textInputAction: textInputAction,
    onChanged: onChanged,
    onFieldSubmitted: onSubmitted,
    validator: tv,
    inputFormatters: inputFormatters,
    focusNode: _focusMap[name],
    onTapOutside: (e) => _focusMap[name]?.unfocus(),
    decoration: InputDecoration(
      prefixIcon: prefixIcon,
      labelText: label,
      errorText: errorText,
      hintText: hint,
      counterText: "",
      border: border,
      suffixIcon: IconButton(
        icon: _eyeMap[name]! ? const Icon(Icons.visibility_off) : const Icon(Icons.visibility),
        onPressed: () {
          _eyeMap[name] = !_eyeMap[name]!;
          onUpdateState();
        },
      ),
    ),
  );
}