【问题标题】:How can I remove the error message below my TextFormField?如何删除 TextFormField 下方的错误消息?
【发布时间】:2021-11-03 12:52:12
【问题描述】:

我正在使用 Flutter 2.5.3,我想删除我的 TextFormField 下方的错误消息。但是无论我做什么,当输入无效时,它都会在输入字段下方添加空间。我试图用 SizedBox 包装它,但它只会在大小盒内调整大小。不过我需要验证器,因为我想显示红色轮廓错误边框。

  Widget _buildEmailForm(BuildContext context) {
    final theme = Theme.of(context);
    final authRepository = context.read<AuthRepository>();

    return Form(
      key: _formKey,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            "signInPageWelcome".tr(),
            style: theme.textTheme.headline1,
          ),
          SizedBox(height: 60),
          TextFormField(
            controller: _emailController,
            validator: (value) {
              final email = Email.dirty(value ?? "");
              return "";
            },
            decoration: InputDecoration(
              prefixIcon: Padding(
                padding: const EdgeInsets.all(30.0),
                child: Icon(
                  Icons.person,
                  color: theme.primaryColor,
                ),
              ),
              hintText: 'signInPageFormEmailFieldHint'.tr(),
              hintStyle: theme.textTheme.bodyText1,
              contentPadding: EdgeInsets.symmetric(vertical: 30, horizontal: 30),
              enabledBorder: OutlineInputBorder(
                borderSide: BorderSide(color: theme.primaryColorLight, width: 1.0),
                borderRadius: BorderRadius.circular(5.0),
              ),
              border: OutlineInputBorder(
                borderSide: BorderSide(color: theme.primaryColorLight, width: 1.0),
                borderRadius: BorderRadius.circular(5.0),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: theme.primaryColorLight, width: 1.0),
                borderRadius: BorderRadius.circular(5.0),
              ),
              errorBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.red, width: 1.0),
                borderRadius: BorderRadius.circular(5.0),
              ),
              errorStyle: TextStyle(color: Colors.transparent, fontSize: 0, height: 0),
            ),
          ),
          SizedBox(height: 60),
          Container(
            width: double.infinity,
            child: TextButton(
              onPressed: () async {
                if (!_formKey.currentState!.validate()) return;
                await authRepository.sendOneTimePassword(email: Email.dirty(_emailController.value.text));
                setState(() {
                  isEmailSent = true;
                });
              },
              child: Padding(
                padding: const EdgeInsets.all(25.0),
                child: Text(
                  "signInPageFormSubmit".tr(),
                  style: theme.textTheme.subtitle1?.copyWith(color: theme.backgroundColor),
                ),
              ),
              style: TextButton.styleFrom(
                elevation: 4,
                backgroundColor: theme.primaryColor,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(180.0),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

编辑

验证器总是有意返回一个字符串,以便我可以查看错误是否隐藏。当我知道它会被隐藏时,我当然会在它有效时返回 null。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您总是收到错误的原因是因为您总是在 validator 中返回一个空字符串 ""

     return ""
    

    要消除错误,您需要返回null

    return null
    

    解决方案:

    使用这个: 验证者:

    (val) => (val == null) ? 'Enter a password' : null
    

    我不明白你是如何定义你的函数dirty,但假设它返回一个bool,你可能需要这样的东西:

     validator: (value) {
                  if (Email.dirty(value)) return "dirty email";
                  else return null;
                },
    

    【讨论】:

    • 进一步澄清。返回 null 表示验证成功,否则任何返回的字符串即使是空字符串也会被视为错误消息。
    • 我明白为什么我总是收到错误,我只想在它出现时隐藏它。因此,当验证器确实返回一个字符串时,它不会显示出来。
    猜你喜欢
    • 2019-10-18
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-08-19
    相关资源
    最近更新 更多