首页 > 解决方案 > TextFormField Validator 在 Flutter 中不起作用

问题描述

我创建了一个页面,他们可以在其中向用户发送验证码。

但是即使我创建了一个 if else 案例,验证器也不起作用。当我单击按钮(即使它是空的)时,它会转到另一个页面。我怎样才能解决这个问题?

这是我的完整代码;

我在最后评论之后将它添加到表单小部件中,但它没有用

   return Scaffold(
        body: SafeArea(
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      IconButton(
                        icon: Icon(Icons.arrow_back),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                      FlatButton(
                        onPressed:(){
                          Navigator.pop(context);
                        },
                        child: Text(
                          allTranslations.text('login'),
                          style: TextStyle(
                            fontSize: 13,
                            fontWeight: FontWeight.w600,
                            color: HexColor('#4A8EB0'),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(bottom: 10),
                  width: double.infinity,
                  child: Text(
                    allTranslations.text('signUp'),
                    style: textTheme.headline5,
                    textAlign: TextAlign.center,
                    maxLines: 3,
                  ),
                ),
          Padding(
            padding: EdgeInsets.all(15),
            child: TextFormField(
              controller: _controller,
              keyboardType: TextInputType.text,
              decoration: CommonInputStyle.textFieldStyle(
                hintTextStr: 'E-mail',
                labelTextStr: 'E-mail',
              ),
              validator: (value) =>
              value.isEmpty ? 'Password cannot be blank' : null,
            ),
          ),
          Padding(
            padding: EdgeInsets.all(16.0),
            child: Container(
              height: 50.0,
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(16),
                color: HexColor('#4A8EB0'),
              ),
              child: InkWell(
                onTap: () async {
                  setState(() {
                    Navigator.pushNamed(context,AnotherPage.routeName);
                  });
                },
                child: Center(
                  child: Text(
                    'Send Code',
                  ),
                ),
              ),
              ],
            ),
          ),
        ),
    );
  }
}

标签: fluttertexttextformfield

解决方案


正如 Limbou 提到的,您需要在用户点击按钮时进行验证。如果成功,它将返回 true。

onTap: () async {
   if(_formKey.currentState.validate()) {
       setState(() {
          Navigator.pushNamed(context, AnotherPage.routeName);
       });
   }
}

推荐阅读