flutter - 如何在验证器中引用表单字段的值或颤振中的另一个表单字段
问题描述
在下面的代码中,我试图根据新密码字段的保存值测试重复密码的值。该值保存到变量_newPW。但在重复密码文本表单字段中,_newPW 变量始终为空。
我该怎么做?我只想验证两个新密码是否匹配。谢谢。
TextFormField(
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'please enter the new password';
}
return null;
},
onSaved: (value) {
_newPW = value;
},
decoration: _textFormFieldDecoration(
hintText: 'new password',
padding: 8.0,
),
),
TextFormField(
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'please enter the new password';
} else if (value != _newPW) {
print('value = $value');
print('newPW = $_newPW'); // <-- _newPW variable is blank
return 'new passwords must match';
}
return null;
},
decoration: _textFormFieldDecoration(
hintText: 'repeat new password',
padding: 8.0,
),
),
解决方案
您可以在下面复制粘贴运行完整代码
您可以使用onChanged
onChanged: (value) {
_newPW = value;
},
工作演示
完整代码
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _newPW;
final _formKey = GlobalKey<FormState>();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Form(
key: _formKey,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'please enter the new password';
}
return null;
},
onChanged: (value) {
_newPW = value;
},
onSaved: (value) {
_newPW = value;
},
decoration: InputDecoration(
hintText: 'new password',
//padding: 8.0,
),
),
TextFormField(
obscureText: true,
validator: (value) {
if (value.isEmpty) {
return 'please enter the new password';
} else if (value != _newPW) {
print('value = $value');
print('newPW = $_newPW'); // <-- _newPW variable is blank
return 'new passwords must match';
}
return null;
},
decoration: InputDecoration(
hintText: 'repeat new password',
//padding: 8.0,
),
),
SubmitWidget(formKey: _formKey)
],
),
),
),
);
}
}
class SubmitWidget extends StatelessWidget {
const SubmitWidget({
Key key,
@required GlobalKey<FormState> formKey,
}) : _formKey = formKey,
super(key: key);
final GlobalKey<FormState> _formKey;
@override
Widget build(BuildContext context) {
return RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
child: Text('Submit'),
);
}
}
推荐阅读
- ruby-on-rails - 如何在rails minitest中使用所需的响应存根发布请求
- python - 禁止直接分配到多对多集合的前端。改用 lab_test.set()
- email - 如何使用 Apps 脚本在电子邮件中获取“批准”、“拒绝”工作的超链接
- c# - 实体框架不适用于外键接口
- flutter - 颤振中的强调色和主色的区别
- php - Codeigniter 试图获取非对象的属性“gambar”
- java - Thorntail 2.6.0.Final WildFly 依赖错误
- laravel - Laravel GuzzleHttp 响应为空
- android - 在谷歌上创建更新服务
- java - 如何将 JTextArea 向右对齐?