Я попытался создать эту форму с проверкой, чтобы она отображала ошибки, когда пользователь возвращает каждое поле. Но по какой-то причине это не работает. У меня нет причин, почему. Я просто застрял сейчас.
Вот код:
import 'package:flutter/material.dart';
import 'package:validate/validate.dart';
void main() => runApp(new MaterialApp(
title: 'Forms in Flutter',
home: new LoginForm(),
theme: ThemeData.dark(),
));
class LoginForm extends StatefulWidget {
String email;
String password;
final Function saveEmail;
final Function savePassword;
final Function attemptLogin;
LoginForm({this.email, this.password, this.saveEmail, this.savePassword,
this.attemptLogin});
@override
LoginFormState createState(){
return new LoginFormState();
}
}
class LoginFormState extends State<LoginForm> {
final loginFormKey = GlobalKey<FormState>();
final emailController = new TextEditingController();
final passController = new TextEditingController();
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Login'),
),
body: new Container(
padding: new EdgeInsets.all(10.0),
child: new Form(
key: loginFormKey,
child: new Column(
children: <Widget>[
new Row(
children: <Widget>[
new Container(
width: 2.0,
height: 18.0,
color: Colors.white,
),
new Container(
width: 5.0,
height: 0.0
),
new Expanded(child: new TextFormField(
decoration: new InputDecoration.collapsed(
hintText: "EMAIL",
),
validator: (String value) {
if (!Validate.isEmail(value)) {
return 'Please enter Email';
}
},
onFieldSubmitted: (val) {
print(loginFormKey.currentState.validate());
if (loginFormKey.currentState.validate()) {
widget.email = val;
widget.saveEmail(val);
}
},
controller: emailController,
),)
],
),
new Row(
children: <Widget>[
new Container(
width: 2.0,
height: 18.0,
color: Colors.white,
padding: const EdgeInsets.fromLTRB(0.0, 0.0, 5.0, 0.0),
),
new Container(
width: 5.0,
height: 0.0
),
new Expanded(child: new TextFormField(
obscureText: true,
decoration: new InputDecoration.collapsed(
hintText: 'PASSWORD',
),
validator: (val) =>
val.length < 6 ?
'Still too short' : '',
onFieldSubmitted: (val) {
if (loginFormKey.currentState.validate()) {
widget.email = emailController.text;
print(widget.email);
widget.saveEmail(emailController.text);
widget.password = val;
print(widget.password);
widget.savePassword(val);
widget.attemptLogin();
}
},
controller: passController,
),)
],
)
],
),
),
)
);
}
}
Я действительно не знаю, что вызвало это. Кажется, что все в onfieldSubmitted часть полей не работает. Если я удаляю операторы If, они работают нормально, но после добавления они не дают ответа.
Кажется, что-то простое, но я просто упускаю суть. Любая помощь будет принята с благодарностью. Благодарю.