0人参与 • 2026-09-18 • flutter
表单处理是任何应用的核心功能之一,flutter 提供了强大的表单处理和验证系统。本文将深入探讨 flutter 表单的各种用法和高级技巧。
form(
key: _formkey,
child: column(
children: [
textformfield(
decoration: inputdecoration(labeltext: '用户名'),
validator: (value) {
if (value == null || value.isempty) {
return '请输入用户名';
}
return null;
},
),
elevatedbutton(
onpressed: () {
if (_formkey.currentstate!.validate()) {
// 表单验证通过
}
},
child: text('提交'),
),
],
),
)
textformfield(
decoration: inputdecoration(labeltext: '邮箱'),
keyboardtype: textinputtype.emailaddress,
validator: (value) {
if (value == null || value.isempty) {
return '请输入邮箱';
}
final emailregex = regexp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailregex.hasmatch(value)) {
return '请输入有效的邮箱地址';
}
return null;
},
)
textformfield(
decoration: inputdecoration(labeltext: '密码'),
obscuretext: true,
validator: (value) {
if (value == null || value.isempty) {
return '请输入密码';
}
if (value.length < 6) {
return '密码长度至少6位';
}
if (!value.contains(regexp(r'[a-z]'))) {
return '密码需要包含大写字母';
}
if (!value.contains(regexp(r'[0-9]'))) {
return '密码需要包含数字';
}
return null;
},
)
final _formkey = globalkey<formstate>();
// 验证表单
if (_formkey.currentstate!.validate()) {
_formkey.currentstate!.save();
}
// 重置表单
_formkey.currentstate!.reset();
class loginform extends statefulwidget {
@override
_loginformstate createstate() => _loginformstate();
}
class _loginformstate extends state<loginform> {
final _formkey = globalkey<formstate>();
string _email = '';
string _password = '';
void _submit() {
if (_formkey.currentstate!.validate()) {
_formkey.currentstate!.save();
// 提交表单
}
}
@override
widget build(buildcontext context) {
return form(
key: _formkey,
child: column(
children: [
textformfield(
decoration: inputdecoration(labeltext: '邮箱'),
onsaved: (value) => _email = value ?? '',
validator: _validateemail,
),
textformfield(
decoration: inputdecoration(labeltext: '密码'),
obscuretext: true,
onsaved: (value) => _password = value ?? '',
validator: _validatepassword,
),
elevatedbutton(
onpressed: _submit,
child: text('登录'),
),
],
),
);
}
}
textformfield(
decoration: inputdecoration(labeltext: '用户名'),
autovalidatemode: autovalidatemode.onuserinteraction,
validator: (value) {
if (value == null || value.isempty) {
return '请输入用户名';
}
if (value.length < 3) {
return '用户名至少3个字符';
}
return null;
},
)
final focusnode _emailfocus = focusnode(); final focusnode _passwordfocus = focusnode(); textformfield( focusnode: _emailfocus, decoration: inputdecoration(labeltext: '邮箱'), textinputaction: textinputaction.next, onfieldsubmitted: (_) => focusscope.of(context).requestfocus(_passwordfocus), ), textformfield( focusnode: _passwordfocus, decoration: inputdecoration(labeltext: '密码'), obscuretext: true, textinputaction: textinputaction.done, onfieldsubmitted: (_) => _submit(), ),
class customtextfield extends statelesswidget {
final string label;
final string? function(string?)? validator;
final void function(string?)? onsaved;
final textinputtype? keyboardtype;
final bool obscuretext;
const customtextfield({
super.key,
required this.label,
this.validator,
this.onsaved,
this.keyboardtype,
this.obscuretext = false,
});
@override
widget build(buildcontext context) {
return textformfield(
decoration: inputdecoration(
labeltext: label,
border: outlineinputborder(
borderradius: borderradius.circular(8),
),
focusedborder: outlineinputborder(
borderradius: borderradius.circular(8),
borderside: borderside(color: colors.blue),
),
),
keyboardtype: keyboardtype,
obscuretext: obscuretext,
validator: validator,
onsaved: onsaved,
);
}
}
customtextfield( label: '邮箱', keyboardtype: textinputtype.emailaddress, validator: _validateemail, onsaved: (value) => _email = value ?? '', ),
class loginform extends statefulwidget {
@override
_loginformstate createstate() => _loginformstate();
}
class _loginformstate extends state<loginform> {
final _formkey = globalkey<formstate>();
final _emailcontroller = texteditingcontroller();
final _passwordcontroller = texteditingcontroller();
bool _isloading = false;
string? _validateemail(string? value) {
if (value == null || value.isempty) {
return '请输入邮箱';
}
final emailregex = regexp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailregex.hasmatch(value)) {
return '请输入有效的邮箱地址';
}
return null;
}
string? _validatepassword(string? value) {
if (value == null || value.isempty) {
return '请输入密码';
}
if (value.length < 6) {
return '密码长度至少6位';
}
return null;
}
future<void> _submit() async {
if (_formkey.currentstate!.validate()) {
setstate(() => _isloading = true);
try {
// 模拟登录请求
await future.delayed(duration(seconds: 2));
// 登录成功
} catch (e) {
// 处理错误
} finally {
setstate(() => _isloading = false);
}
}
}
@override
widget build(buildcontext context) {
return form(
key: _formkey,
child: padding(
padding: edgeinsets.all(16),
child: column(
children: [
textformfield(
controller: _emailcontroller,
decoration: inputdecoration(
labeltext: '邮箱',
prefixicon: icon(icons.email),
border: outlineinputborder(
borderradius: borderradius.circular(8),
),
),
keyboardtype: textinputtype.emailaddress,
validator: _validateemail,
),
sizedbox(height: 16),
textformfield(
controller: _passwordcontroller,
decoration: inputdecoration(
labeltext: '密码',
prefixicon: icon(icons.lock),
border: outlineinputborder(
borderradius: borderradius.circular(8),
),
),
obscuretext: true,
validator: _validatepassword,
),
sizedbox(height: 24),
_isloading
? circularprogressindicator()
: elevatedbutton(
onpressed: _submit,
child: text('登录'),
style: elevatedbutton.stylefrom(
minimumsize: size(double.infinity, 48),
shape: roundedrectangleborder(
borderradius: borderradius.circular(8),
),
),
),
],
),
),
);
}
}
class registerform extends statefulwidget {
@override
_registerformstate createstate() => _registerformstate();
}
class _registerformstate extends state<registerform> {
final _formkey = globalkey<formstate>();
final _emailcontroller = texteditingcontroller();
final _passwordcontroller = texteditingcontroller();
final _confirmpasswordcontroller = texteditingcontroller();
bool _passwordvisible = false;
string? _validateemail(string? value) {
if (value == null || value.isempty) return '请输入邮箱';
final emailregex = regexp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!emailregex.hasmatch(value)) return '请输入有效的邮箱地址';
return null;
}
string? _validatepassword(string? value) {
if (value == null || value.isempty) return '请输入密码';
if (value.length < 6) return '密码长度至少6位';
if (!value.contains(regexp(r'[a-z]'))) return '密码需要包含大写字母';
if (!value.contains(regexp(r'[0-9]'))) return '密码需要包含数字';
return null;
}
string? _validateconfirmpassword(string? value) {
if (value == null || value.isempty) return '请确认密码';
if (value != _passwordcontroller.text) return '两次输入的密码不一致';
return null;
}
@override
widget build(buildcontext context) {
return form(
key: _formkey,
child: padding(
padding: edgeinsets.all(16),
child: column(
children: [
textformfield(
controller: _emailcontroller,
decoration: inputdecoration(
labeltext: '邮箱',
prefixicon: icon(icons.email),
border: outlineinputborder(borderradius: borderradius.circular(8)),
),
keyboardtype: textinputtype.emailaddress,
validator: _validateemail,
),
sizedbox(height: 16),
textformfield(
controller: _passwordcontroller,
decoration: inputdecoration(
labeltext: '密码',
prefixicon: icon(icons.lock),
suffixicon: iconbutton(
icon: icon(_passwordvisible ? icons.visibility : icons.visibility_off),
onpressed: () => setstate(() => _passwordvisible = !_passwordvisible),
),
border: outlineinputborder(borderradius: borderradius.circular(8)),
),
obscuretext: !_passwordvisible,
validator: _validatepassword,
),
sizedbox(height: 16),
textformfield(
controller: _confirmpasswordcontroller,
decoration: inputdecoration(
labeltext: '确认密码',
prefixicon: icon(icons.lock),
border: outlineinputborder(borderradius: borderradius.circular(8)),
),
obscuretext: !_passwordvisible,
validator: _validateconfirmpassword,
),
sizedbox(height: 24),
elevatedbutton(
onpressed: () {
if (_formkey.currentstate!.validate()) {
// 提交注册
}
},
child: text('注册'),
style: elevatedbutton.stylefrom(
minimumsize: size(double.infinity, 48),
shape: roundedrectangleborder(borderradius: borderradius.circular(8)),
),
),
],
),
),
);
}
}
class formsubmitbutton extends statelesswidget {
final bool isloading;
final voidcallback onpressed;
final string text;
const formsubmitbutton({
super.key,
required this.isloading,
required this.onpressed,
required this.text,
});
@override
widget build(buildcontext context) {
return elevatedbutton(
onpressed: isloading ? null : onpressed,
child: isloading
? row(
mainaxisalignment: mainaxisalignment.center,
children: [
circularprogressindicator(size: 20),
sizedbox(width: 8),
text('处理中...'),
],
)
: text(text),
style: elevatedbutton.stylefrom(
minimumsize: size(double.infinity, 48),
shape: roundedrectangleborder(borderradius: borderradius.circular(8)),
),
);
}
}
a:使用 reset 方法:
_formkey.currentstate!.reset();
a:使用 onsaved 回调或 texteditingcontroller:
// 方法一:onsaved textformfield( onsaved: (value) => _email = value ?? '', ) // 方法二:texteditingcontroller final _emailcontroller = texteditingcontroller(); string email = _emailcontroller.text;
a:使用 focusnode:
final _focusnode = focusnode();
@override
void initstate() {
super.initstate();
widgetsbinding.instance.addpostframecallback((_) {
focusscope.of(context).requestfocus(_focusnode);
});
}
textformfield(
focusnode: _focusnode,
)
// 推荐 final _controller = texteditingcontroller(); textformfield( controller: _controller, ) // 不推荐 textformfield( onsaved: (value) => _email = value ?? '', )
// 推荐
string? _validateemail(string? value) {
if (value == null || value.isempty) return '请输入邮箱';
final regex = regexp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$');
if (!regex.hasmatch(value)) return '请输入有效的邮箱';
return null;
}
// 不推荐
textformfield(
validator: (value) {
// 验证逻辑直接写在这里
},
)
// 推荐
bool _isloading = false;
elevatedbutton(
onpressed: _isloading ? null : _submit,
child: _isloading ? circularprogressindicator() : text('提交'),
)
flutter 的表单处理系统非常强大和灵活。通过本文的学习,你应该能够:
掌握这些技巧,能够帮助你构建更加健壮和用户友好的表单。
到此这篇关于flutter表单处理与验证各种用法和高级技巧的文章就介绍到这了,更多相关flutter表单处理与验证内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论