简体   繁体   English

我想在 Flutter 中验证我的 textFormField 而不改变装饰

[英]I want to validate my textFormField in Flutter without changing the decoration

I have a textfield in flutter with border decoration.我在 flutter 中有一个带有边框装饰的文本字段。 the text field also have a validator property.My problem is that when i click on submit and the validation error message is shown the text field border goes off.I really don't know any work around about this.文本字段也有一个验证器属性。我的问题是,当我单击提交并显示验证错误消息时,文本字段边框消失了。我真的不知道有什么解决方法。

be low is my code:低是我的代码:

class _RegisterPageState extends State<RegisterPage> {
  final _key = GlobalKey<FormState>();
  static const textDeco = OutlineInputBorder(
    borderSide: BorderSide(color: Colors.blue),
    borderRadius: BorderRadius.all(
      Radius.circular(25),
    ),
  );
  String _email;

  Widget _buildEmail() {
    return TextFormField(
      decoration: InputDecoration(
        // labelText: 'Email',
        enabledBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.transparent),
          borderRadius: BorderRadius.all(
            Radius.circular(25),
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(color: Colors.blue),
          borderRadius: BorderRadius.all(
            Radius.circular(25),
          ),
        ),
        prefixIcon: Icon(Icons.enhanced_encryption),
        hintText: "Email",
        filled: true,
        fillColor: Colors.grey[200],
      ),
      validator: (String value) {
        if (value.isEmpty) {
          return "Username is required";
        } else if (value.length <= 5) {
          return "Username should be greater than 5";
        } else {
          return null;
        }
      },
      onSaved: (String value) {
        _email = value;
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Column(
      children: <Widget>[
        Form(
          key: _key,
          child: Padding(
            padding: const EdgeInsets.all(50.0),
            child: Column(
              children: <Widget>[
                _buildEmail(),

                FlatButton(
                  child: Text("Submit"),
                  onPressed: () {
                    //
                    if (_key.currentState.validate()) {
                      print("sunmitted");
                    }
                  },
                ),
              ],
            ),
          ),
        )
      ],
    ));
  }
}

and my output looks like this:我的 output 看起来像这样:

Normal interface普通界面

the error message output looks like this: Error interface错误消息 output 如下所示:错误界面

You need to set errorBorder and focusedErrorBorder to your TextFormField.您需要将errorBorderfocusedErrorBorder设置为您的TextFormField。

errorBorder: OutlineInputBorder(
   borderSide: BorderSide(color: Colors.red),
   borderRadius: BorderRadius.circular(25.0),
),
focusedErrorBorder: OutlineInputBorder(
   borderSide: BorderSide(color: Colors.red),
   borderRadius: BorderRadius.circular(25.0),
),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM