Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.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
Android 如何将验证器传递到“TextFormField';飘飘然?_Android_Ios_Dart_Flutter - Fatal编程技术网

Android 如何将验证器传递到“TextFormField';飘飘然?

Android 如何将验证器传递到“TextFormField';飘飘然?,android,ios,dart,flutter,Android,Ios,Dart,Flutter,在我的flatter应用程序中,我正在为我的表单动态生成文本字段。请检查下面的代码 Widget _buildInputFields( String label, TextEditingController textController, TextInputType textInputType, IconData icon, Color iconColor, ) { return Container( margin: EdgeI

在我的flatter应用程序中,我正在为我的表单动态生成文本字段。请检查下面的代码

Widget _buildInputFields(
    String label,
    TextEditingController textController,
    TextInputType textInputType,
    IconData icon,
    Color iconColor,
  ) {
    return Container(
        margin: EdgeInsets.only(left: 20, bottom: 20),
        child: Container(
          padding: EdgeInsets.only(right: 20),
          child: Row(
            children: <Widget>[
              Flexible(
                child: TextFormField(
                  controller: textController,
                  validator: (value) {
                    if (value.isEmpty) {
                      return 'Please enter some text';
                    }
                  },
                  style: new TextStyle(color: Colors.white),
                  keyboardType: textInputType,
                  decoration: InputDecoration(
                      labelText: label,
                      fillColor: Colors.white,
                      labelStyle: TextStyle(
                          color: Colors.white, fontWeight: FontWeight.w600),
                      enabledBorder: OutlineInputBorder(
                        borderSide:
                            const BorderSide(color: Colors.white30, width: 2.0),
                        borderRadius: BorderRadius.circular(25.0),
                      ),
                      suffixIcon: IconButton(
                        icon: Icon(icon, color: iconColor),
                        onPressed: () {},
                      )),
                ),
              ),
            ],
          ),
        ));
  }
Widget\u buildInputFields(
字符串标签,
text编辑控制器text控制器,
TextInputType TextInputType,
Iconda图标,
颜色iconColor,
) {
返回容器(
页边距:仅限边集(左:20,下:20),
子:容器(
填充:仅限边缘设置(右:20),
孩子:排(
儿童:[
灵活的(
子项:TextFormField(
控制器:textController,
验证器:(值){
if(value.isEmpty){
返回“请输入一些文本”;
}
},
样式:新文本样式(颜色:Colors.white),
键盘类型:textInputType,
装饰:输入装饰(
标签文本:标签,
fillColor:Colors.white,
标签样式:文本样式(
颜色:Colors.white,fontWeight:fontWeight.w600),
enabledBorder:OutlineInputBorder(
边界:
const BorderSide(颜色:Colors.white30,宽度:2.0),
边界半径:边界半径。圆形(25.0),
),
后缀:图标按钮(
图标:图标(图标,颜色:iconColor),
按下:(){},
)),
),
),
],
),
));
}
上面的方法返回一个带有我需要的样式的
TextFormField
,因此我不必对它进行数百次重新编码。我只需调用该方法,就会得到一个新的
TextFormField


无论如何,我需要进行表单验证,每个字段都有不同的验证。在Flatter中,我如何将
验证程序
传递给
textformfield

由于您已经在使用验证程序,我想您只需要将其作为
\u buildInputFields
中的参数传递,对吗

可能是这样的:

Widget\u buildInputFields(
...
颜色iconColor,
函数验证器,
) {
...
子项:TextFormField(
控制器:textController,
验证器:验证器,
样式:新文本样式(颜色:Colors.white),
...
}
你可以用它,你会很好的

但是,您可以更具体地使用validator
函数
类型,如下所示:

Widget\u buildInputFields(
...
颜色iconColor,
FormFieldValidator验证程序,
...
因此,您可以将验证器定义为
State
类的方法并重用它们,或者直接在
\u buildInputFields
调用中指定它们

在下面的示例中,您有一个字段Name,它使用同一类中定义的方法_notEmptyValidator。由于LastName遵循相同的逻辑,它将重用此方法

。。。
字符串_notEmptyValidator(字符串值){
if(value.isEmpty){
返回“请输入一些文本”;
}
}
...
纵队(
儿童:[
_buildInputFields(“名称”,\u notEmptyValidator),
_buildInputFields(“姓氏”,\u notEmptyValidator),
文本“:空),
]
...
在下面的示例中,我保留了以前的字段,但添加了一个新字段。这个新字段有一个非常特定的验证逻辑,我将在
\u buildInputFields
调用中定义验证方法,因此不会在其他字段中重用它

。。。
纵队(
儿童:[
_buildInputFields(“名称”,\u notEmptyValidator),
_buildInputFields(“姓氏”,\u notEmptyValidator),
文本“:空),
_buildInputFields(“有效数字”,(值){
if(double.tryParse(值)==null){
返回“请输入有效数字”;
}
},
]
...

您只需将验证器作为参数传递,就像传递给其他验证器一样。您只需传入一个函数,该函数将字符串作为参数并返回字符串

//username validator possible structure
   Function(String) usernameValidator = (String username){
        if(username.isEmpty){
          return 'Username empty';
        }else if(username.length < 3){
          return 'Username short';
        }

        return null;
  };

  //password validator possible structure
  passwordValidator(String password){
        if(password.isEmpty){
          return 'Password empty';
        }else if(password.length < 3){
          return 'PasswordShort';
        }
        return null;
  }  



 //new build function
Widget _buildInputFields(
    String label,
    TextEditingController textController,
    TextInputType textInputType,
    IconData icon,
    Color iconColor,
    String Function(String) validator
  ) {
    return Container(
        margin: EdgeInsets.only(left: 20, bottom: 20),
        child: Container(
          padding: EdgeInsets.only(right: 20),
          child: Row(
            children: <Widget>[
              Flexible(
                child: TextFormField(
                  controller: textController,
                  validator: validator,
                  style: new TextStyle(color: Colors.white),
                  keyboardType: textInputType,
                  decoration: InputDecoration(
                      labelText: label,
                      fillColor: Colors.white,
                      labelStyle: TextStyle(
                          color: Colors.white, fontWeight: FontWeight.w600),
                      enabledBorder: OutlineInputBorder(
                        borderSide:
                            const BorderSide(color: Colors.white30, width: 2.0),
                        borderRadius: BorderRadius.circular(25.0),
                      ),
                      suffixIcon: IconButton(
                        icon: Icon(icon, color: iconColor),
                        onPressed: () {},
                      )),
                ),
              ),
            ],
          ),
        ));
  }

    //calling your function
   _buildInputFields(label, textController, textInputType, icon, iconColor, usernameValidator);
   _buildInputFields(label, textController, textInputType, icon, iconColor, passwordValidator);
//用户名验证程序可能的结构
函数(字符串)usernameValidator=(字符串用户名){
if(username.isEmpty){
返回“用户名为空”;
}else if(username.length<3){
返回'Username short';
}
返回null;
};
//密码验证程序可能的结构
密码验证程序(字符串密码){
if(password.isEmpty){
返回“密码为空”;
}else if(password.length<3){
返回“PasswordShort”;
}
返回null;
}  
//新建功能
Widget\u buildInputFields(
字符串标签,
text编辑控制器text控制器,
TextInputType TextInputType,
Iconda图标,
颜色iconColor,
字符串函数(字符串)验证器
) {
返回容器(
页边距:仅限边集(左:20,下:20),
子:容器(
填充:仅限边缘设置(右:20),
孩子:排(
儿童:[
灵活的(
子项:TextFormField(
控制器:textController,
验证器:验证器,
样式:新文本样式(颜色:Colors.white),
键盘类型:textInputType,
装饰:输入装饰(
标签文本:标签,
fillColor:Colors.white,
标签样式:文本样式(
颜色:Colors.white,fontWeight:fontWeight.w600),
enabledBorder:OutlineInputBorder(
边界:
const BorderSide(颜色:Colors.white30,宽度:2.0),
边界半径:边界半径。圆形(25.0),
),
TextFormField(
  // The validator receives the text that the user has entered.
  validator: (value) {
    return myMethod(value, context, ...);
  },
  //... other attributes
)