Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap TextFormField validator messages in Flutter?

Tags:

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;
                                },
                              )),
like image 845
Chickgen Avatar asked Dec 31 '22 12:12

Chickgen


1 Answers

Use errorMaxLines in InputDecoration(), like this:

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;
  },
)
like image 123
royarg Avatar answered Feb 01 '23 09:02

royarg