我有一个 textformfield 验证最小长度至少为 3 个字符。稍后,我将使用后端验证用户名是唯一的。
但目前,我无法为 2 个错误显示 2 个错误文本。即,一个用于最小长度(“请为您的用户名输入至少 3 个字符”)和一个用于非唯一用户名(“您的用户名很受欢迎,请尝试另一个。”)
问题:
如何使后缀图标仅在输入至少 1 个字符时出现?而不是默认情况下,我知道它链接到 hasMinLengthUnique = false。
如何为各个错误显示不同的错误文本?
以及稍后,如何根据值 hasMinLengthUnique = true 在导航到另一个页面上放置一个三元运算符。
Bcos 现在,无论 hasMinLengthUnique = true 还是 false,导航都不会发生。
Form(
key: _formKey,
autovalidateMode: AutovalidateMode.always,
child: TextFormField(
controller: _controller,
validator: (value) {
return (0 < value.length && value.length < 3)
? "Please enter at least 3 characters for your username"
: null;
},
onChanged: (value) {
setState(() {
value.length > 2
// && username must be unique
? hasMinLengthUnique = true
: hasMinLengthUnique = false;
});
},
style: TextStyle(color: Colors.white),
autofocus: true,
maxLength: 15,
decoration: InputDecoration(
suffix: hasMinLengthUnique
? IconButton(
icon:
Icon(Icons.check_circle, color: Colors.green))
: IconButton(
icon: Icon(Icons.cancel, color: Colors.red)),
errorBorder: UnderlineInputBorder(
borderSide: BorderSide(color: Colors.blue, width: 1.0),
),
errorStyle: TextStyle(color: Color(0xff4aa3f8)),
focusedErrorBorder: UnderlineInputBorder(
borderSide: BorderSide.none,
),
border: OutlineInputBorder(),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(15)),
// borderSide: BorderSide(width: 1, color: Colors.white),
),
),
),
),
),
),
SizedBox(
height: MediaQuery.of(context).size.height * 0.18,
),
Padding(
padding: EdgeInsets.only(
right: MediaQuery.of(context).size.height * 0.015,
),
child: DelayedDisplay(
delay: Duration(seconds: 3),
child: Align(
alignment: Alignment.centerRight,
child: ElevatedButton(
style: ElevatedButton.styleFrom(
onPrimary: Colors.white,
primary: Color(0xff3a327f)),
child: Icon(Icons.chevron_right, size: 27),
onPressed: () async {
// if (formKey.currentState.validate()) {
Navigator.push(context, _createRoute());
// }
})),
))
]),
));