我有一个颤动的TextFormField,当文本为空或为空时,我已经显示错误并且轮廓边框颜色变为红色。我想要的是suffixIcon
当TextFormField为空时也显示
下面是我的代码
class Login extends StatefulWidget {
const Login({Key? key}) : super(key: key);
@override
_LoginState createState() => _LoginState();
}
class _LoginState extends State<Login> {
final loginFormKey = GlobalKey<FormState>();
TextEditingController userNameController = TextEditingController();
FocusNode userNameFocusNode = FocusNode();
bool userNameHasFocus = false;
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
userNameController.dispose();
userNameFocusNode.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
body: SingleChildScrollView(
child: Center(
child: Form(
autovalidateMode: AutovalidateMode.onUserInteraction,
key: loginFormKey,
child: Column(
children: [
Container(
padding: const EdgeInsets.all(1),
margin: const EdgeInsets.only(top: 5, bottom: 5),
child: const Text(
"Welcome to my app",
textAlign: TextAlign.center,
)),
Container(
margin: const EdgeInsets.only(
top: 1, bottom: 2, right: 1, left: 10),
child: TextFormField(
focusNode: userNameFocusNode,
controller: userNameController,
decoration: InputDecoration(
labelText: "Please Enter Username",
border: OutlineInputBorder(),
),
validator: (phoneNo) {
if (phoneNo!.isEmpty) {
userNameFocusNode.requestFocus();
userNameHasFocus = true;
return "You must enter username";
} else {
userNameHasFocus = false;
return null;
}
},
)),
Container(
margin: const EdgeInsets.only(
top: 2, bottom: 1, right: 10, left: 10),
child: ElevatedButton(
onPressed: () {
if (loginFormKey.currentState!.validate()) {
print(proceed)
}
},
child: Text("Login"),
style: ElevatedButton.styleFrom(
shape: StadiumBorder(),
)))
],
)))),
);
}
}
我感兴趣的部分在下面validator
validator: (phoneNo) {
if (phoneNo!.isEmpty) {
userNameFocusNode.requestFocus();
userNameHasFocus = true;
return "You must enter username";
} else {
userNameHasFocus = false;
return null;
}
},
如果phoneNo!.isEmpty
我应该在下面显示类似的内容
suffixIcon: Icon(Icons.error_outline_rounded, color: Colors.red)