gpt4 book ai didi

Flutter TextEditingController clear 不会重置错误信息

转载 作者:行者123 更新时间:2023-12-04 08:28:58 25 4
gpt4 key购买 nike

我使用 flutter 表单 autovalidateMode: AutovalidateMode.onUserInteraction 来验证文本字段是否未填写。

如何在提交表单并重新设置文本域时隐藏验证错误消息?

下面是我的代码来演示我的问题,填写文本字段并按提交后。还会显示红色错误消息,这对用户体验不友好。

DartPad 链接:https://dartpad.dev/d26c3f57be04acaafae2ee127a688e3f

代码

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>();
final TextEditingController taskController = TextEditingController();

@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: taskController,
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
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 and reset
Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
taskController.clear();
}
},
child: Text('Submit'),
),
),
],
),
);
}
}

最佳答案

我们可以使用 InputDecoration 来切换 errorStyle 并获得所需的结果。

输出:

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

// 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>();
final TextEditingController taskController = TextEditingController();
bool hideError = false;
bool first = true;

@override
Widget build(BuildContext context) {
// Build a Form widget using the _formKey created above.
return Form(
key: _formKey,
autovalidateMode: AutovalidateMode.onUserInteraction,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
TextFormField(
controller: taskController,
decoration: hideError && !first
? InputDecoration(
errorStyle: TextStyle(height: 0),
)
: null,
validator: (value) {
if (value.isEmpty) {
print(value);

return 'Please enter some text';
}
return null;
},
onChanged: (value) {
setState(() {
hideError = false;
first = false;
});
}),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed: () {
// Validate returns true if the form is valid, or false
// otherwise.
setState(() {
hideError = true;
});
if (_formKey.currentState.validate()) {
// If the form is valid, display a Snackbar and reset

Scaffold.of(context)
.showSnackBar(SnackBar(content: Text('Processing Data')));
taskController.clear();

}
},
child: Text('Submit'),
),
),
],
),
);
}
}

关于Flutter TextEditingController clear 不会重置错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65120125/

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