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),
);
}
flutter 获取一个 右边有清除按钮 的TextField
最新推荐文章于 2024-07-08 22:04:26 发布