Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振-文本字段验证don';t工作_Flutter_Dart - Fatal编程技术网

Flutter 颤振-文本字段验证don';t工作

Flutter 颤振-文本字段验证don';t工作,flutter,dart,Flutter,Dart,我需要做的是,当onPressed被调用时,当我不输入文本时,我会得到Textfield错误 class _ExampleDialogTextState extends State<ExampleDialogText> { FocusNode focusNode = FocusNode(); final textController = TextEditingController(); bool noText = false; String nameList = ""

我需要做的是,当onPressed被调用时,当我不输入文本时,我会得到Textfield错误

class _ExampleDialogTextState extends State<ExampleDialogText> {
  FocusNode focusNode = FocusNode();
  final textController = TextEditingController();
  bool noText = false;
  String nameList = "";

  @override
  void initState() {
    super.initState();
    nameList = "";
    focusNode.addListener(() {
      if (!focusNode.hasFocus) {
        setState(() {
          noText = nameList.length == 0;
        });
        FocusScope.of(context).requestFocus(focusNode);
      }
    });
  }

TextField(
focusNode: focusNode,
autofocus: true,
controller: textController,
style: TextStyle(
color: Colors.black, fontSize: 14),
decoration: InputDecoration(
counterText: '',
errorText:
noText ? 'Value Can\'t Be Empty' : null,)

RaisedButton(
onPressed: () {
setState(() {
nameList.isEmpty
? noText = true
: noText = false;
});
},)

}
class\u示例DialogTextState扩展状态{
FocusNode FocusNode=FocusNode();
final textController=TextEditingController();
bool noText=false;
字符串名称列表=”;
@凌驾
void initState(){
super.initState();
姓名列表=”;
focusNode.addListener(){
如果(!focusNode.hasFocus){
设置状态(){
noText=nameList.length==0;
});
FocusScope.of(context).requestFocus(focusNode);
}
});
}
文本字段(
focusNode:focusNode,
自动对焦:对,
控制器:textController,
样式:TextStyle(
颜色:颜色。黑色,字体大小:14),
装饰:输入装饰(
计数器文本:“”,
错误文本:
noText?“值不能为空”:null,)
升起的按钮(
已按下:(){
设置状态(){
nameList.isEmpty
?noText=true
:noText=false;
});
},)
}

但是,使用这些代码对我来说仍然不起作用


谢谢!

您的代码是正确的,但无法在ShowDialog小部件中更新状态,因此您必须在ShowDialog中返回有状态的小部件

我添加了我更改的全部代码

import 'package:flutter/material.dart';

class Consts {
  Consts._();

  static const double padding = 16.0;
  static const double buttonPadding = 5.0;
}

class DeleteWidget extends StatefulWidget {
  const DeleteWidget({Key key}) : super(key: key);

  @override
  _DeleteWidgetState createState() => _DeleteWidgetState();
}

class _DeleteWidgetState extends State<DeleteWidget> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueAccent,
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialogNameList();
        },
        backgroundColor: Colors.orange,
        child: Icon(
          Icons.add,
          color: Colors.purple,
          size: 40,
        ),
      ),
    );
  }

  showDialogNameList() {
    return showDialog(
        context: context,
        builder: (context) {
          return CustomeDialog1();
        });
  }
}

class CustomeDialog1 extends StatefulWidget {
  CustomeDialog1({Key key}) : super(key: key);

  @override
  _CustomeDialog1State createState() => _CustomeDialog1State();
}

class _CustomeDialog1State extends State<CustomeDialog1> {
  FocusNode focusNode = FocusNode();
  final textController = TextEditingController();
  bool noText = false;
  String nameList = "";

  @override
  void initState() {
    super.initState();
    nameList = "";
    focusNode.addListener(() {
      if (!focusNode.hasFocus) {
        setState(() {
          noText = nameList.length == 0;
        });
        FocusScope.of(context).requestFocus(focusNode);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    var screenHeight = MediaQuery.of(context).size.height;

    return Dialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(Consts.padding),
        ),
        elevation: 0.0,
        child: Container(
          height: screenHeight / 3,
          child: Stack(
            children: <Widget>[
              Container(
                padding: EdgeInsets.only(
                  top: Consts.padding,
                  bottom: Consts.padding,
                  left: Consts.padding,
                  right: Consts.padding,
                ),
                margin: EdgeInsets.only(top: 0),
                decoration: BoxDecoration(
                  color: Colors.white,
                  shape: BoxShape.rectangle,
                  borderRadius: BorderRadius.circular(Consts.padding),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black26,
                      blurRadius: 10.0,
                      offset: const Offset(0.0, 10.0),
                    ),
                  ],
                ),
                child: Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: <Widget>[
                      TextField(
                        focusNode: focusNode,
                        autofocus: true,
                        controller: textController,
                        cursorColor: Colors.white,
                        style: TextStyle(color: Colors.black, fontSize: 14),
                        decoration: InputDecoration(
                          counterText: '',
                          errorText: noText ? 'Value Can\'t Be Empty' : null,
                          hintText: 'List Name',
                          enabledBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.black),
                          ),
                          focusedBorder: OutlineInputBorder(
                            borderSide: BorderSide(color: Colors.green),
                          ),
                          labelStyle: TextStyle(
                              color: Colors.white, fontWeight: FontWeight.bold),
                        ),
                        onChanged: (String text) {
                          nameList = text;
                        },
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: <Widget>[
                            Container(
                              width: 150.0,
                              height: 45.0,
                              child: RaisedButton(
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(10),
                                ),
                                onPressed: () {
                                  setState(() {
                                    nameList.isEmpty
                                        ? noText = true
                                        : noText = false;
                                  });
                                },
                                padding:
                                    EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                                color: Color(0xFF2DA771),
                                child: Text('Add',
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontFamily: 'Roboto',
                                        fontSize: 16)),
                              ),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              )
            ],
          ),
        ));
  }
}
导入“包装:颤振/材料.省道”;
类常数{
常数;
静态常数双填充=16.0;
静态常数双按钮添加=5.0;
}
类DeleteWidget扩展了StatefulWidget{
constdeleteWidget({Key}):超级(Key:Key);
@凌驾
_DeleteWidgetState createState()=>\u DeleteWidgetState();
}
类_DeleteWidgetState扩展状态{
@凌驾
小部件构建(构建上下文){
返回脚手架(
背景颜色:Colors.blueAccent,
浮动操作按钮:浮动操作按钮(
已按下:(){
showDialogNameList();
},
背景颜色:Colors.orange,
子:图标(
Icons.add,
颜色:颜色,紫色,
尺码:40,
),
),
);
}
showDialogNameList(){
返回显示对话框(
上下文:上下文,
生成器:(上下文){
返回CustomeDialog1();
});
}
}
类CustomeDialog1扩展StatefulWidget{
CustomeDialog1({Key}):超级(Key:Key);
@凌驾
_CustomeDialog1State createState()=>_CustomeDialog1State();
}
类_CustomeDialog1State扩展状态{
FocusNode FocusNode=FocusNode();
final textController=TextEditingController();
bool noText=false;
字符串名称列表=”;
@凌驾
void initState(){
super.initState();
姓名列表=”;
focusNode.addListener(){
如果(!focusNode.hasFocus){
设置状态(){
noText=nameList.length==0;
});
FocusScope.of(context).requestFocus(focusNode);
}
});
}
@凌驾
小部件构建(构建上下文){
var screenHeight=MediaQuery.of(context).size.height;
返回对话框(
形状:圆形矩形边框(
边界半径:边界半径。圆形(常量填充),
),
标高:0.0,
子:容器(
高度:屏幕高度/3,
子:堆栈(
儿童:[
容器(
填充:仅限边缘设置(
顶部:常量填充,
底部:常量填充,
左:常量填充,
右:常量填充,
),
边距:仅限边集(顶部:0),
装饰:盒子装饰(
颜色:颜色,白色,
形状:BoxShape.rectangle,
边界半径:边界半径。圆形(常量填充),
boxShadow:[
箱形阴影(
颜色:颜色。黑色,
半径:10.0,
偏移量:常数偏移量(0.0,10.0),
),
],
),
儿童:中心(
子:列(
mainAxisAlignment:mainAxisAlignment.spaceAround,
儿童:[
文本字段(
focusNode:focusNode,
自动对焦:对,
控制器:textController,
光标颜色:颜色。白色,
样式:TextStyle(颜色:Colors.black,字体大小:14),
装饰:输入装饰(
计数器文本:“”,
errorText:noText?“值不能为空”:null,
hintText:'列表名称',
enabledBorder:OutlineInputBorder(
borderSide:borderSide(颜色:Colors.black),
),
聚焦顺序:大纲输入边框(
borderSide:borderSide(颜色:Colors.green),
),
标签样式:文本样式(
颜色:Colors.white,fontwweight:fontwweight.bold),
),
onChanged:(字符串文本){
名称列表=文本;
},
),
填充物(
填充:常数边集全部(8.0),
孩子:排(
crossAxisAlignment:crossAxisAlignment.center,
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
容器(
宽度:150.0,
身高:45.0,
孩子:升起按钮(
形状:圆形矩形边框(
边界半径:边界半径。圆形(10),
),
已按下:(){