简体   繁体   English

如何在 Flutter 中包装 TextFormField 验证器消息?

[英]How to wrap TextFormField validator messages in Flutter?

I want to increase the length of the validator message whilst keeping the width of the textfield itself the same.我想增加验证器消息的长度,同时保持文本字段本身的宽度相同。 Ideally it would just wrap onto a second line but I have no idea how to achieve this.理想情况下,它只会换成第二行,但我不知道如何实现这一点。

This is my current implementation.这是我目前的实现。

Expanded(
                              flex: 1,
                              child: TextFormField(
                                controller: shortNameController,
                                decoration: InputDecoration(hintText: 'SHRT'),
                                textAlign: TextAlign.center,
                                textCapitalization: TextCapitalization.characters,
                                inputFormatters: [
                                  WhitelistingTextInputFormatter(
                                      RegExp('[A-Za-z0-9]')),
                                  LengthLimitingTextInputFormatter(4),

                                ],
                                validator: (value) {
                                  if (value.isEmpty) {
                                    return 'Enter your team\'s short name';
                                  } else if (value.length < 3) {
                                    return 'Must be at least 3 characters';
                                  }
                                  return null;
                                },
                              )),

Use errorMaxLines in InputDecoration() , like this:InputDecoration()中使用errorMaxLines ,如下所示:

TextFormField(
  controller: shortNameController,
  decoration: InputDecoration(
    hintText: 'SHRT',
    errorMaxLines: 3, // number of lines the error text would wrap 
  ),

  // other parameters

  validator: (value) {
    if (value.isEmpty) {
      return 'Enter your team\'s short name';
    } else if (value.length < 3) {
      return 'Must be at least 3 characters';
    }
    return null;
  },
)

You can use line breaks in your validator message to go to the next line您可以在验证器消息中使用换行符到 go 到下一行

'Must be at least 3 \ncharacters'; '必须至少 3 个\n字符';

The result will be:结果将是:

Must be at least 3
characters.

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

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