gpt4 book ai didi

flutter - Flutter Textform验证未显示

转载 作者:行者123 更新时间:2023-12-03 04:20:30 28 4
gpt4 key购买 nike

我看过一些关于如何使验证器起作用的教程,但是似乎都没有。谁能帮我这个?这是一个简单的登录页面的代码。如果应该检测到任何类型的错误,我的验证器不会显示在屏幕上。我看过教程,它以红色显示,但是在我的应用中,它根本没有显示。

 class UserLogin extends StatefulWidget {

UserLogin({this.auth,this.onSignedIn});
final BaseAuth auth;
final VoidCallback onSignedIn;
@override
State<StatefulWidget> createState()=> _UserLoginState();
}

class _UserLoginState extends State<UserLogin> {

final formkey = GlobalKey<FormState>();

bool _validateAndSave()
{
final form = formkey.currentState;
if(form.validate())
{
form.save();
return true;
}
else
return false;
}

static final incorrect_icon = Icon(
Icons.error,
color: Colors.pink,
);

void _validateAndSubmit() async
{

if(_validateAndSave()) {
try {
String userId = await widget.auth.signIn(emailid, password);
print('Signed in! $userId');
//widget.onSignedIn();
Navigator.push(context, MaterialPageRoute(builder: (context)=>Feed()));
}
catch (e) {
print('Error: $e');
}
}

}



static final TextEditingController emailContr = new TextEditingController();
static final TextEditingController passwordContr = new TextEditingController();

static String get emailid => emailContr.text;
static String get password => passwordContr.text;

final _email = Container(
padding: EdgeInsets.only(left: 10, right: 10),
child: TextFormField(
keyboardType: TextInputType.emailAddress,
controller: emailContr,
autofocus: false,
validator: (input) {
if(input.isEmpty)
{
return 'Email cannot be empty';
}
return null;
},
//onSaved: (input)=> emailid = input,
decoration: InputDecoration(
hintText: 'Enter Email Address',
suffixIcon: Icon(Icons.email),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)
),
),
),
);

final _pass = Container(
padding: EdgeInsets.only(left: 10, right: 10),
child: TextFormField(
controller: passwordContr,
obscureText: true,
autofocus: false,
validator: (input) {
if(input.length <= 6)
{
return 'Password should be at least 6 characters';
}
return null;
},
decoration: InputDecoration(
hintText: 'Enter password',
suffixIcon: Icon(Icons.lock),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10)
),
),
),
);

/*final login_button =

},
);
*/

@override
Widget build(BuildContext context) {
return new Scaffold(
backgroundColor: Colors.yellow,
body: Container(
child: Form(
key: formkey,
child: Column(
children: <Widget>[
SizedBox(height: 200,),
Text('Vibing',
style:TextStyle(
fontWeight: FontWeight.bold,
fontSize: 64,
),
),
SizedBox(height: 100,),
_email,
SizedBox(height: 20,),
_pass,
SizedBox(height:30),
RaisedButton(

color: Colors.yellow,
elevation: 5,
child: Text('Login'),
onPressed: (){
_validateAndSubmit();
formkey.currentState.reset();
}
),
SizedBox(height:10),
FlatButton(
child: Text('Forgot password'),
onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder:(context)=>ForgotPassword()),)
),
SizedBox(height:10),
FlatButton(
child: Text('New? Register here!'),
onPressed: ()=> Navigator.push(context, MaterialPageRoute(builder:(context)=>UserReg()),)
),
],
),
),
) ,
);
}

}

最佳答案

问题是您在验证后重置了表单,因此显示的任何错误都将重置。只需从登录按钮回调中删除此行:

formkey.currentState.reset();
瞧:
enter image description here

关于flutter - Flutter Textform验证未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62890969/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com