【问题标题】:How to show Text at bottom of textformfield after user enter some data in it用户在其中输入一些数据后如何在 textformfield 底部显示文本
【发布时间】:2021-11-25 16:02:38
【问题描述】:

这是我的 UI 代码

Container(
           padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
           child:TextFormField(
             controller: name,
           textInputAction: TextInputAction.next,
           decoration: InputDecoration(
             prefixIcon: Icon(
               Icons.person,
             ),
            hintText: "First name",
             border: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.purpleAccent),
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
    ),
            Container(
           padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
           child:TextFormField( 
             controller: second_name,
           textInputAction: TextInputAction.next,
           decoration: InputDecoration(
             prefixIcon: Icon(
               Icons.person,
             ),
            hintText: "Second Name",
             border: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.purpleAccent),
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
    ), 
            Container(
           padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
           child:TextFormField(
           textInputAction: TextInputAction.next,
           decoration: InputDecoration(
             prefixIcon: Icon(
               Icons.mail,
             ),
            hintText: "Email-id ",
             border: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.purpleAccent),
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
    ), Container(
           padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
           child:TextFormField(
           textInputAction: TextInputAction.next,
           decoration: InputDecoration(
             prefixIcon: Icon(
               Icons.vpn_key,
             ),
            hintText: "Password",
             border: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.purpleAccent),
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
    ), Container(
           padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
           child:TextFormField(
           textInputAction: TextInputAction.next,
           decoration: InputDecoration(
             prefixIcon: Icon(
               Icons.vpn_key,
             ),
            hintText: "Confirm Password",
             border: OutlineInputBorder(
            borderSide: BorderSide(color: Colors.purpleAccent),
            borderRadius: BorderRadius.circular(10),
          ),
        ),
      ),
    ),

现在有一个注册按钮,现在我想在用户输入一些数据后在每个容器底部显示一个文本,如果数据不符合某些给定条件,例如“密码不匹配” ,那么如何在我的按钮的 onpressed 函数中显示或执行什么。 请为此提供一些代码。 提前致谢。

这是一个示例图片,所以红色的文字说“这个 eamil 不存在....”如何显示这样的文字..

【问题讨论】:

  • 您想在输入或点击登录按钮时显示错误吗?

标签: flutter dart flutter-onpressed


【解决方案1】:

您可以使用 onChanged 属性。

onChanged: (text) {
   
  }

【讨论】:

  • 对不起,先生,我不明白这将如何显示我想要的文本@akram
【解决方案2】:

在 TextFormField 中,有一个验证器函数。您可以使用它来显示错误消息。

  Container(
       padding: EdgeInsets.fromLTRB(10, 10, 10, 0),
       child:TextFormField(
         controller: name,
       validator: (value){
       if(value== null || value.isEmpty){
       return 'Name must not be empty';
       }
       return null;
       }
       textInputAction: TextInputAction.next,
       decoration: InputDecoration(
         prefixIcon: Icon(
           Icons.person,
         ),
        hintText: "First name",
         border: OutlineInputBorder(
        borderSide: BorderSide(color: Colors.purpleAccent),
        borderRadius: BorderRadius.circular(10),
      ),
    ),
  ),
),

您还可以将整个屏幕包装在 Form 小部件中,并使用 key: 属性来验证用户何时单击按钮。您仍然必须在 TextFormField 中传递验证器函数。表单小部件只是验证器功能特性的扩展。

 final _formKey = GlobalKey<FormState>();

 Form(
 key: _formKey,
 )

然后在你的按钮中添加 onChanged 这个代码,这样如果没有验证表单就不会提交。

 onChanged: () {
              if (!_formKey.currentState!.validate()) {
              return;
             }
            // write code to save inputs here
            }

或者, 您还可以使用验证器功能在 TextFormField 中添加 autovalidateMode: AutovalidateMode.always 来验证用户交互的表单。此属性在表单小部件中也可用。

【讨论】:

    【解决方案3】:

    请参考以下代码

    您可以尝试任何一种解决方案

    解决方案 1:自动验证被禁用,并且仅在用户交互时,即单击验证按钮文本表单字段时相应地开始验证。

    class MainScreen extends StatelessWidget {
      MainScreen({Key key}) : super(key: key);
    
      final TextEditingController addressController = TextEditingController();
      final FocusNode addressFocus = FocusNode();
      final TextEditingController stateController = TextEditingController();
      final FocusNode stateFocus = FocusNode();
      final _validationKey = GlobalKey<FormState>();
    
      int validateAddress(String address) {
        String patttern = r'(^[a-zA-Z0-9 ,.-]*$)';
        RegExp regExp = new RegExp(patttern);
        if (address.isEmpty || address.length == 0) {
          return 1;
        } else if (address.length < 10) {
          return 3;
        } else {
          return 0;
        }
      }
    
      int validateState(String state) {
        String patttern = r'(^[a-zA-Z0-9 ,.-]*$)';
        RegExp regExp = new RegExp(patttern);
        if (state.isEmpty || state.length == 0) {
          return 1;
        } else {
          return 0;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.lightBlue,
            automaticallyImplyLeading: true,
            leading: Icon(
              Icons.arrow_back,
            ),
            title: Text("Example"),
            centerTitle: true,
          ),
          body: Container(
            padding: EdgeInsets.all(15.0),
            child: Column(
              children: [
                Form(
                  key: _validationKey,
                  child: Column(
                    children: [
                      /* State */
                      TextFormField(
                        autovalidateMode: AutovalidateMode.onUserInteraction,
                        /* autovalidate is disabled */
                        controller: stateController,
                        inputFormatters: [
                          FilteringTextInputFormatter.deny(RegExp(r"\s\s")),
                          FilteringTextInputFormatter.deny(RegExp(
                              r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])')),
                        ],
                        keyboardType: TextInputType.text,
                        maxLength: 160,
                        onChanged: (val) {},
                        maxLines: 1,
                        validator: (value) {
                          int res = validateAddress(value);
                          if (res == 1) {
                            return "Please enter state";
                          } else {
                            return null;
                          }
                        },
                        focusNode: stateFocus,
                        autofocus: false,
                        decoration: InputDecoration(
                          errorMaxLines: 3,
                          counterText: "",
                          filled: true,
                          fillColor: Colors.white,
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          disabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                            ),
                          ),
                          errorBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(Radius.circular(4)),
                              borderSide: BorderSide(
                                width: 1,
                                color: Colors.red,
                              )),
                          focusedErrorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Colors.red,
                            ),
                          ),
                          hintText: "Enter state" ?? "",
                        ),
                      ),
                      SizedBox(
                        height: 15.0,
                      ),
    
                      /* Address */
                      TextFormField(
                        autovalidateMode: AutovalidateMode.onUserInteraction,
                        /* autovalidate is disabled */
                        controller: addressController,
                        inputFormatters: [
                          FilteringTextInputFormatter.deny(RegExp(r"\s\s")),
                          FilteringTextInputFormatter.deny(RegExp(
                              r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])')),
                        ],
                        keyboardType: TextInputType.text,
                        maxLength: 60,
                        onChanged: (val) {},
                        maxLines: 1,
                        validator: (value) {
                          int res = validateAddress(value);
                          if (res == 1) {
                            return "Please enter address";
                          } else if (res == 3) {
                            return "Please enter minimum 10 characters";
                          } else {
                            return null;
                          }
                        },
                        focusNode: addressFocus,
                        autofocus: false,
                        decoration: InputDecoration(
                          errorMaxLines: 3,
                          counterText: "",
                          filled: true,
                          fillColor: Colors.white,
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          disabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                            ),
                          ),
                          errorBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(Radius.circular(4)),
                              borderSide: BorderSide(
                                width: 1,
                                color: Colors.red,
                              )),
                          focusedErrorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Colors.red,
                            ),
                          ),
                          hintText: "Enter address" ?? "",
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 15.0,
                ),
                OutlinedButton(
                  onPressed: () {
                    _validationKey.currentState.validate();
                    if (stateController.text.isEmpty) {
                      stateFocus.requestFocus();
                    } else if (addressController.text.isEmpty ||
                        addressController.text.length < 10) {
                      addressFocus.requestFocus();
                    }
                  },
                  child: Text("Validate"),
                )
              ],
            ),
          ),
        );
      }
    }
    
    

    解决方案 2:始终启用自动验证

    class HomeScreen extends StatelessWidget {
      HomeScreen({Key key}) : super(key: key);
    
      final TextEditingController addressController = TextEditingController();
      final FocusNode addressFocus = FocusNode();
      final TextEditingController stateController = TextEditingController();
      final FocusNode stateFocus = FocusNode();
      final _validationKey = GlobalKey<FormState>();
    
      int validateAddress(String address) {
        String patttern = r'(^[a-zA-Z0-9 ,.-]*$)';
        RegExp regExp = new RegExp(patttern);
        if (address.isEmpty || address.length == 0) {
          return 1;
        } else if (address.length < 10) {
          return 3;
        } else {
          return 0;
        }
      }
    
      int validateState(String state) {
        String patttern = r'(^[a-zA-Z0-9 ,.-]*$)';
        RegExp regExp = new RegExp(patttern);
        if (state.isEmpty || state.length == 0) {
          return 1;
        } else {
          return 0;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.lightBlue,
            automaticallyImplyLeading: true,
            leading: Icon(
              Icons.arrow_back,
            ),
            title: Text("Example"),
            centerTitle: true,
          ),
          body: Container(
            padding: EdgeInsets.all(15.0),
            child: Column(
              children: [
                Form(
                  key: _validationKey,
                  child: Column(
                    children: [
                      /* State */
                      TextFormField(
                        autovalidateMode: AutovalidateMode.always,
                        /* autovalidate is set to true */
                        controller: stateController,
                        inputFormatters: [
                          FilteringTextInputFormatter.deny(RegExp(r"\s\s")),
                          FilteringTextInputFormatter.deny(RegExp(
                              r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])')),
                        ],
                        keyboardType: TextInputType.text,
                        maxLength: 160,
                        onChanged: (val) {},
                        maxLines: 1,
                        validator: (value) {
                          int res = validateAddress(value);
                          if (res == 1) {
                            return "Please enter state";
                          } else {
                            return null;
                          }
                        },
                        focusNode: stateFocus,
                        autofocus: false,
                        decoration: InputDecoration(
                          errorMaxLines: 3,
                          counterText: "",
                          filled: true,
                          fillColor: Colors.white,
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          disabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                            ),
                          ),
                          errorBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(Radius.circular(4)),
                              borderSide: BorderSide(
                                width: 1,
                                color: Colors.red,
                              )),
                          focusedErrorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Colors.red,
                            ),
                          ),
                          hintText: "Enter state" ?? "",
                        ),
                      ),
                      SizedBox(
                        height: 15.0,
                      ),
    
                      /* Address */
                      TextFormField(
                        autovalidateMode: AutovalidateMode.always,
                        /* autovalidate is enabled */
                        controller: addressController,
                        inputFormatters: [
                          FilteringTextInputFormatter.deny(RegExp(r"\s\s")),
                          FilteringTextInputFormatter.deny(RegExp(
                              r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])')),
                        ],
                        keyboardType: TextInputType.text,
                        maxLength: 60,
                        onChanged: (val) {},
                        maxLines: 1,
                        validator: (value) {
                          int res = validateAddress(value);
                          if (res == 1) {
                            return "Please enter address";
                          } else if (res == 3) {
                            return "Please enter minimum 10 characters";
                          } else {
                            return null;
                          }
                        },
                        focusNode: addressFocus,
                        autofocus: false,
                        decoration: InputDecoration(
                          errorMaxLines: 3,
                          counterText: "",
                          filled: true,
                          fillColor: Colors.white,
                          focusedBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          disabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          enabledBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Color(0xffE5E5E5),
                            ),
                          ),
                          border: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                            ),
                          ),
                          errorBorder: OutlineInputBorder(
                              borderRadius: BorderRadius.all(Radius.circular(4)),
                              borderSide: BorderSide(
                                width: 1,
                                color: Colors.red,
                              )),
                          focusedErrorBorder: OutlineInputBorder(
                            borderRadius: BorderRadius.all(Radius.circular(4)),
                            borderSide: BorderSide(
                              width: 1,
                              color: Colors.red,
                            ),
                          ),
                          hintText: "Enter address" ?? "",
                        ),
                      ),
                    ],
                  ),
                ),
                SizedBox(
                  height: 15.0,
                ),
              ],
            ),
          ),
        );
      }
    
    

    【讨论】:

      猜你喜欢
      • 2022-12-06
      • 2020-05-31
      • 1970-01-01
      • 2014-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 2021-06-09
      相关资源
      最近更新 更多