static TextField textField(
{TextEditingController? controller,
String? hintText,
bool obscureText = false,
ValueChanged<String>? onChanged}) {
// 是否添加清除按钮
bool hasSuffixIcon = false;
if (controller != null && controller.text.isNotEmpty) {
// 有内容,添加清除按钮
hasSuffixIcon = true;
}
return TextField(
controller: controller,
obscureText: obscureText, // 密码输入框 obscureText = true
onChanged: (value) {
if (onChanged != null) {
onChanged(value);
}
},
decoration: InputDecoration(
hintText: hintText, // 输入提示text
suffixIcon: hasSuffixIcon
? IconButton(
// 点击清除按钮
onPressed: () {
controller!.text = '';
if (onChanged != null) {
onChanged(controller.text);
}
},
icon: Icon(Icons.clear))
: null),
);
}
06-08