06153
06153

Reputation: 339

Multiline string for TextFormString validator

In my TextFormString for Password Field I have validator that returns a String. The problem is this String is too long and it doesn't fit the screen.

I'd like to make it multiline, but I can't find how to do it: I've tried setting width for the Container this TextFormString is in -- no effect, I've hardcoded newlines \n to my String, it actually worked but I think there must be some other solution to break it to lines more dynamically.

What is the right way to do it?

Screenshot

Upvotes: 17

Views: 9159

Answers (2)

Adnan K
Adnan K

Reputation: 79

I think the accepted answer is deprecated. Now you can add the errorMaxLines in InputDecoration like this

TextFormField(
  decoration: InputDecoration(
    errorMaxLines: 2,
  ),
  validator: (value) {
    if (value.isEmpty) {
      return 'Please Enter Something';
    }
    return null;
  },
)

Upvotes: 2

shadowsheep
shadowsheep

Reputation: 15012

You can decorate your TextFormFiled so that the error label has more than 1 lines:

errorMaxLines: 2

Here an example:

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;
    },
),

enter image description here

In this example I didn't set obscureText: true (desiderata for a password field), so that the text is visible.

Upvotes: 49

Related Questions