gpt4 book ai didi

Flutter Form Autovalidate 首次触发

转载 作者:行者123 更新时间:2023-12-04 13:15:07 36 4
gpt4 key购买 nike

我在第一次启动应用程序时出现错误,所有文本字段都在验证,但不能像这样,我从 flutter 文档中获取了表单示例,并添加了 autovalidate:true,显然在网络上它可以工作,但具有物理设备和模拟器它不工作,错误和代码如下
enter image description here

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(),
),
);
}
}

class MyCustomForm extends StatefulWidget {
@override
MyCustomFormState createState() {
return MyCustomFormState();
}
}


class MyCustomFormState extends State<MyCustomForm> {

final _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {

return Form(
autovalidate:true,
key: _formKey,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
}
},
child: Text('Submit'),
),
),
],
),
);
}
}

最佳答案

停止首次自动验证我们应该使用 autovalidateMode: AutovalidateMode.onUserInteraction, 而不是 自动验证=真 .自 自动验证 已弃用。所以像这样使用:-
将 GlobalKey 定义为:

final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
在脚手架上使用这个。
child: new Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: FormUI(),
),

关于Flutter Form Autovalidate 首次触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61293478/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com