【问题标题】:Flutter - How to change border of text form field on errorFlutter - 如何在错误时更改文本表单字段的边框
【发布时间】:2020-01-23 14:37:10
【问题描述】:

每当表单无效时,它会自动将所有无效字段的边框变为红色。我怎样才能改变它的颜色呢......让我们说白色。

【问题讨论】:

    标签: flutter flutter-layout


    【解决方案1】:

    嗯,我刚刚找到了答案。这就是你要做的:

        new InputDecoration(
            errorBorder: new OutlineInputBorder(
                borderSide: new BorderSide(color: Colors.white, width: 0.0),
            ),
            focusedErrorBorder: new OutlineInputBorder(
                borderSide: new BorderSide(color: Colors.blue, width: 0.0),
            ),
        );
    

    【讨论】:

      【解决方案2】:
      TextFormField(
            decoration: InputDecoration(
                errorBorder:
                    OutlineInputBorder(borderSide: BorderSide(color: Colors.white))),
          );
      

      【讨论】:

      • 您好,这很有用。我在 Flutter 的文档中找不到自己。 InputBorder 和 OutlineInputBorder 有什么区别? inputBorder 只是一个抽象函数吗?
      【解决方案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,
                  ),
                ],
              ),
            ),
          );
        }
      }
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-21
        • 1970-01-01
        • 2021-05-13
        • 2022-11-04
        • 2013-03-06
        • 2021-08-28
        • 2023-01-31
        • 2021-04-05
        相关资源
        最近更新 更多