我对在Flutter上注册Google帐户的TextField实施感兴趣。我该如何从三个对象都具有一个errorText的日期开始制作一系列类似的TextField,然后当它们单击“下一步”时,立即检查三个,如果没有输入,即使它们是正确的,所有内容也会变成红色。就像三者之一。
答案 0 :(得分:0)
对于大纲文本字段,您可以使用
TextField(
decoration: new InputDecoration(
border: new OutlineInputBorder(),
filled: true,
hintText: "Type in your text",
),
)
进行验证的最佳方法是使用form with validation
和TextFormField
代替TextField
示例:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appTitle = 'Form Validation Demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(
title: Text(appTitle),
),
body: MyCustomForm(),
),
);
}
}
// Create a Form widget.
class MyCustomForm extends StatefulWidget {
@override
MyCustomFormState createState() {
return MyCustomFormState();
}
}
// Create a corresponding State class.
// This class holds data related to the form.
class MyCustomFormState extends State<MyCustomForm> {
// Create a global key that uniquely identifies the Form widget
// and allows validation of the form.
//
// Note: This is a GlobalKey<FormState>,
// not a GlobalKey<MyCustomFormState>.
final _formKey = GlobalKey<FormState>();
String _firstName;
String _lastName;
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onSaved: (val) => _firstName = val,
),
TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onSaved: (val) => _lastName = val,
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
final form = _formKey.currentState;
if (form.validate()) {
form.save();
// If the form is valid, display a Snackbar.
Scaffold.of(context).showSnackBar(SnackBar(
content: Text('The result: $_firstName, $_lastName')));
}
},
child: Text('Submit'),
),
),
],
),
);
}
}