Flutter 文本字段错误文本未显示
我正在做一个基本的应用程序,但它不能正常工作。我有一个文本字段,但我不希望它是空的。我有一个文本控制器,我使用errorText,但它不能正常工作。 Mi代码为:Flutter 文本字段错误文本未显示,flutter,dart,Flutter,Dart,我正在做一个基本的应用程序,但它不能正常工作。我有一个文本字段,但我不希望它是空的。我有一个文本控制器,我使用errorText,但它不能正常工作。 Mi代码为: void changeDesc() { showDialog( context: context, builder: (context) { return AlertDialog( title: Text('New description'), cont
void changeDesc() { showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text('New description'),
content: TextField(
controller: _textController,
decoration: InputDecoration(
hintText: "description",
errorText: descriptionIncorrect
? 'Description cannot be empty'
: null,
),
),
actions: <Widget>[
FlatButton(
child: new Text('Ok'),
onPressed: () {
setState(() {_textController.text.length == 0
? descriptionIncorrect= true
: descriptionIncorrect= false;});
if (_textController.text.length != 0) {
alert.description = _textController.text;
Navigator.of(context).pop();
}
},
),
FlatButton(
child: new Text('Cancel'),
onPressed: () {
setState(() {
_textController.text.length == 0
? descriptionIncorrect= true
: descriptionIncorrect= false;});
if (_textController.text.length == 0) {
_textController.text = alert.description;
Navigator.of(context).pop();
}
},
)
],
);
});}
void changeDesc(){showDialog(
上下文:上下文,
生成器:(上下文){
返回警报对话框(
标题:文本(“新说明”),
内容:TextField(
控制器:_textController,
装饰:输入装饰(
hintText:“描述”,
errorText:描述不正确
?“说明不能为空”
:null,
),
),
行动:[
扁平按钮(
子项:新文本('Ok'),
已按下:(){
setState((){u textController.text.length==0
?descriptionIncorrect=真
:descriptionIncorrect=false;});
如果(_textController.text.length!=0){
alert.description=\u textController.text;
Navigator.of(context.pop();
}
},
),
扁平按钮(
子项:新文本(“取消”),
已按下:(){
设置状态(){
_textController.text.length==0
?descriptionIncorrect=真
:descriptionIncorrect=false;});
如果(_textController.text.length==0){
_textController.text=alert.description;
Navigator.of(context.pop();
}
},
)
],
);
});}
当我按下OK按钮,文本字段为空时,错误应该出现,但不会出现。我试过一些方法,但不能按我需要的那样做
谢谢。您需要执行两个步骤1-要刷新警报对话框,您需要将其包装在
StatefulBuilder
小部件中。2-将自动对焦添加到文本字段。
这是完整的代码
bool descriptionIncorrect = false;
var _textController = TextEditingController();
void changeDesc() {
showDialog(
context: context,
builder: (context) {
return StatefulBuilder(
builder: (context, setState) {
return AlertDialog(
title: Text('New description'),
content: TextField(
autofocus: true,
controller: _textController,
decoration: InputDecoration(
hintText: "description",
errorText: descriptionIncorrect
? 'Description cannot be empty'
: null,
),
),
actions: <Widget>[
FlatButton(
child: new Text('Ok'),
onPressed: () {
setState(() {
_textController.text.length == 0
? descriptionIncorrect = true
: descriptionIncorrect = false;
print(_textController.text.length.toString());
print(descriptionIncorrect.toString());
});
if (_textController.text.length != 0) {
alert.description = _textController.text;
Navigator.of(context).pop();
}
},
),
FlatButton(
child: new Text('Cancel'),
onPressed: () {
setState(() {
_textController.text.length == 0
? descriptionIncorrect = true
: descriptionIncorrect = false;
});
if (_textController.text.length == 0) {
_textController.text = alert.description;
Navigator.of(context).pop();
}
},
)
],
);
},
);
});
}
bool descriptionIncorrect=false;
var_textController=TextEditingController();
void changeDesc(){
显示对话框(
上下文:上下文,
生成器:(上下文){
返回状态生成器(
生成器:(上下文,设置状态){
返回警报对话框(
标题:文本(“新说明”),
内容:TextField(
自动对焦:对,
控制器:_textController,
装饰:输入装饰(
hintText:“描述”,
errorText:描述不正确
?“说明不能为空”
:null,
),
),
行动:[
扁平按钮(
子项:新文本('Ok'),
已按下:(){
设置状态(){
_textController.text.length==0
?descriptionIncorrect=真
:descriptionIncorrect=false;
打印(_textController.text.length.toString());
打印(descriptionIncorrect.toString());
});
如果(_textController.text.length!=0){
alert.description=\u textController.text;
Navigator.of(context.pop();
}
},
),
扁平按钮(
子项:新文本(“取消”),
已按下:(){
设置状态(){
_textController.text.length==0
?descriptionIncorrect=真
:descriptionIncorrect=false;
});
如果(_textController.text.length==0){
_textController.text=alert.description;
Navigator.of(context.pop();
}
},
)
],
);
},
);
});
}