构建带验证的表单
如何构建一个验证输入的表单。
应用程序通常需要用户在文本字段中输入信息。例如,你可能需要用户使用电子邮件地址和密码组合登录。
为了使应用程序安全且易于使用,请检查用户提供的信息是否有效。如果用户正确填写了表单,则处理该信息。如果用户提交了不正确的信息,则显示友好的错误消息,告知他们出了什么问题。
在本示例中,学习如何使用以下步骤将验证添加到具有单个文本字段的表单中
- 创建一个带有
GlobalKey的Form。 - 添加带有验证逻辑的
TextFormField。 - 创建一个按钮来验证并提交表单。
1. 使用 GlobalKey 创建 Form
#
创建一个 Form。Form 组件充当容器,用于分组和验证多个表单字段。
创建表单时,提供一个 GlobalKey。这将为你的 Form 分配一个唯一的标识符。它还允许你在稍后验证表单。
将表单创建为 StatefulWidget。这允许你一次创建一个唯一的 GlobalKey<FormState>()。然后你可以将其存储为变量并在不同的点访问它。
如果你将此组件设为 StatelessWidget,则需要将此 key 存储在某个地方。由于它占用资源较多,因此你不想每次运行 build 方法时都生成一个新的 GlobalKey。
import 'package:flutter/material.dart';
// Define a custom Form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@override
MyCustomFormState createState() {
return MyCustomFormState();
}
}
// Define 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>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: const Column(
children: <Widget>[
// Add TextFormFields and ElevatedButton here.
],
),
);
}
}
2. 添加带有验证逻辑的 TextFormField
#
虽然 Form 已经就位,但它没有让用户输入文本的方式。这就是 TextFormField 的作用。TextFormField 组件呈现一个 Material Design 文本字段,并在发生验证错误时显示它们。
通过将 validator() 函数提供给 TextFormField 来验证输入。如果用户的输入无效,则 validator 函数返回一个包含错误消息的 String。如果没有错误,则验证器必须返回 null。
对于此示例,创建一个 validator,以确保 TextFormField 不为空。如果为空,则返回一条友好的错误消息。
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
3. 创建一个按钮来验证并提交表单
#现在你有一个带有文本字段的表单,提供一个用户可以点击以提交信息的按钮。
当用户尝试提交表单时,检查表单是否有效。如果是,则显示成功消息。如果不是(文本字段没有内容),则显示错误消息。
ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text('Submit'),
),
这是如何工作的?
#要验证表单,请使用在步骤 1 中创建的 _formKey。你可以使用 _formKey.currentState 访问器来访问 FormState,该状态由 Flutter 在构建 Form 时自动创建。
FormState 类包含 validate() 方法。调用 validate() 方法时,它将为表单中的每个文本字段运行 validator() 函数。如果一切正常,则 validate() 方法返回 true。如果任何文本字段包含错误,则 validate() 方法会重建表单以显示任何错误消息并返回 false。
互动示例
#import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
const appTitle = 'Form Validation Demo';
return MaterialApp(
title: appTitle,
home: Scaffold(
appBar: AppBar(title: const Text(appTitle)),
body: const MyCustomForm(),
),
);
}
}
// Create a Form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@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>();
@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextFormField(
// The validator receives the text that the user has entered.
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false otherwise.
if (_formKey.currentState!.validate()) {
// If the form is valid, display a snackbar. In the real world,
// you'd often call a server or save the information in a database.
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Processing Data')),
);
}
},
child: const Text('Submit'),
),
),
],
),
);
}
}
要了解如何检索这些值,请查看 检索文本字段的值 教程。