TextFormString 验证器的多行字符串

061*_*153 4 dart flutter

在我TextFormString的密码字段中,我有validator返回一个字符串。问题是这String太长了,它不适合屏幕。

我想使它成为多行,但我找不到如何去做:我已经尝试为Containerthis设置宽度TextFormString- 没有效果,我已经将换行符硬编码\n到我的字符串中,它实际上有效,但我认为必须有其他解决方案可以更动态地将其分解为行。

正确的做法是什么?

截屏

sha*_*eep 15

您可以装饰您TextFormFiled的错误标签,使错误标签多于 1 行:

errorMaxLines: 2
Run Code Online (Sandbox Code Playgroud)

这里有一个例子:

TextFormField(
    decoration: const InputDecoration(
    icon: Icon(Icons.person),
    hintText: 'What do people call you?',
    labelText: 'Name *',
    errorMaxLines: 2
    ),
    validator: (String value) {
    return value.contains('@')
        ? 'Do not use the @ char. Do not use the @ char. Do not use the @ char. Do not use the @ char.'
        : null;
    },
),
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在这个例子中,我没有设置obscureText: true(desiderata for a password field),所以文本是可见的。

  • 如果错误消息是动态的,这意味着我们不确定错误消息的长度怎么办? (2认同)